home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 60.zip / BS1 part 60 / LSD 31.adf / AmigaMachineLang1.pp / AmigaMachineLang1
Text File  |  1990-09-07  |  154KB  |  2,926 lines

  1.               _____          _________________________________
  2.              /    /\        /                                 \
  3.             /    / /       /    ___________________________    \
  4.            /    / /       /    / _________________________/\    \
  5.           /    / /       /    /_/___________      _____   \ \    \
  6.          /    / /       /                   \    /\    \   \ \    \
  7.         /    / /       /_________________    \   \ \    \   \ \    \
  8.        /    / /        \________________/\    \   \ \    \   \ \    \
  9.       /    /_/__________________________\_\    \   \ \    \___\/     \
  10.      /                                          \   \ \              / 
  11.     /____________________________________________\   \ \____________/
  12.     \____________________________________________/    \/___________/FISH
  13.  
  14.                                 P R E S E N T
  15.  
  16.                  AMIGA MACHINE LANGUAGE - FROM ABACUS BOOKS
  17.  
  18.                                  P A R T  I
  19.  
  20.  
  21.                      Typed by DEE JAY of X-CELL for LSD
  22.  
  23.  
  24.  
  25.  
  26.       Table of Contents.
  27.       ------------------
  28.         1.     Introduction
  29.         1.1    Why machine code?
  30.         1.2    A look into the Amiga's memory
  31.         1.2.1  RAM,ROM,hardware register
  32.         1.2.2  Bits,bytes and words
  33.         1.2.3  Number systems
  34.         1.3    Inside the Amiga
  35.         1.3.1  Components and libraries
  36.         1.3.2  Memory
  37.         1.3.3  Multi-tasking
  38.  
  39.         2      The MC68000 processor
  40.         2.1    Registers
  41.         2.2    Addressing memory
  42.         2.3    Operating modes
  43.         2.3.1  User and supervisor modes
  44.         2.3.2  Exceptions
  45.         2.3.3  Interrupts
  46.         2.3.4  Condition codes
  47.         2.4    The 68000 Instructions
  48.  
  49.         3      Working with assemblers
  50.         3.1    The development assembler
  51.         3.2    AssemPro
  52.         3.3    The K-SEKA assembler
  53.  
  54.         4      Our first programs
  55.         4.1    Adding tables
  56.         4.2    Sorting tables
  57.         4.3    Converting number systems
  58.         4.3.1  Converting hex to ASCII
  59.         4.3.2  Converting decimal to ASCII
  60.         4.3.3  Converting ASCII to hex
  61.         4.3.4  Converting ASCII to decimal
  62.  
  63.         5      Hardware registers
  64.         5.1    Checking for special keys
  65.         5.2    Timing
  66.         5.3    Reading the mouse or joystick
  67.         5.4    Tone production
  68.         5.5    Hardware registers overview
  69.  
  70.         6      The Amiga operating system
  71.         6.1    Load libraries
  72.         6.2    Calling functions
  73.         6.3    Program initialization
  74.         6.3.1  Reserve memory
  75.         6.3.2  Opening a simple window
  76.         6.4    Input/output
  77.         6.4.1  Screen output
  78.         6.4.2  Keyboard input
  79.         6.4.3  Printer control
  80.         6.4.4  Serial I/O
  81.         6.4.5  Speech output
  82.         6.5    Disk operations
  83.         6.5.1  Open files
  84.         6.5.2  Reading and writing data
  85.         6.5.3  Erase files
  86.         6.5.4  Rename files
  87.         6.5.5  CLI directory
  88.         6.5.6  Read directory
  89.         6.5.7  Direct access to disk
  90.  
  91.         7      Working with Intuition
  92.         7.1    Open screen
  93.         7.2    Openwindow
  94.         7.3    Requesters
  95.         7.4    Event handling
  96.         7.5    Menu programming
  97.         7.6    Text output
  98.         7.7    Images
  99.         7.8    Borders
  100.         7.9    Gadgets
  101.         7.9.1  Boolean gadgets
  102.         7.9.2  String gadgets
  103.         7.9.3  Proportional gadgets
  104.         7.10   Example program
  105.  
  106.         8      Advanced programming
  107.         8.1    Supervisor mode
  108.         8.2    Exception programming
  109.  
  110.         Appendix
  111.                Overview of library functions
  112.                Overview of the MC68000 Instruction
  113.                
  114.  
  115.  
  116.  
  117.       CHAPTER 1
  118.       ---------
  119.       1.Introduction.
  120.       --------------  
  121.         Before you tackle machine language,you should take a closer
  122.         look at several things that are vital to machine language
  123.         programming.
  124.     
  125.       1.1.Why Machine Language.
  126.       ------------------------
  127.         Machine language is actually the only language the MC68000
  128.         processor understands.All other languages,such as Basic,Pascal
  129.         or C,must first be translated(interpreted or compiled) into
  130.         machine code.This process can take place either when the 
  131.         program is executed(the BASIC interpreter),or before program
  132.         execution(the Pascal and C compilers).
  133.       Advantages;
  134.         The great advantage of machine language over an interpreted
  135.         and compiled program is machine language programs are faster.
  136.         With an interpreter like BASIC,each line must first be 
  137.         interpreted before it is executed,which requires a great deal
  138.         of time.A Pascal or C compiler translates the source into
  139.         machine language.This translation procedure does not produce
  140.         programs that are as fast as pure machine language programs.
  141.         Another advantage machine language has over BASIC is that an
  142.         interpreter is not needed for the execution of a machine
  143.         language program.
  144.         Machine language can access all the capabilities of the 
  145.         computor since it is the language native to the computor.It
  146.         is possible that machine subroutines are required by a higher
  147.         level language to access functions that aren't directly 
  148.         accessible by that language.
  149.       
  150.       1.2.A Look Into The Amiga's Memory.
  151.       ----------------------------------
  152.         Before a machine language program can be written,you must 
  153.         know exactly what the program is required to do.You must also
  154.         be aware of what resources are needed and available to achieve
  155.         those goals.The most important of these resources is the 
  156.         memory in the Amiga.
  157.  
  158.       1.2.1.RAM,ROM,Hardware Register.
  159.       -------------------------------  
  160.         Random Access Memory,referred to as RAM,allows information to
  161.         be placed in it and then withdrawn at a later time.This memory
  162.         consists of electronic componants that retain data only while
  163.         the computor is turned on(or until power failure).
  164.         So that the computor is able to do something when it is first
  165.         turned on,such as promting the Workbench or Kickstart disk,a
  166.         program has to remain in memory when the power is off.A memory
  167.         type which can retain data in memory without any power being
  168.         needed.This second memory type is known as ROM.
  169.       ROM;
  170.         ROM stands for Read Only Memory,indicating that data can only
  171.         be read from this memory,not written to it.The Amiga contains
  172.         a ROM,that loads the Workbench or Kickstart disk into RAM.The
  173.         first version of the Amiga did not contain the Kickstart in
  174.         ROM.
  175.       PROM;
  176.         One variation of ROM is the PROM,or Programmable Read Only
  177.         Memory.This special type of ROM can actually be programmed
  178.         once.Since it cannot be erased once programmed,it isn't
  179.         encountered very often.More often you will see EPROM's. or
  180.         Erasable Programmable ROM's.These special chips,which can be
  181.         erased with ultraviolet light,have a little window on the
  182.         surface of the chip usually covered with tape.
  183.       EEROM;
  184.         Although not available on the consumer market and much more 
  185.         expensive than RAM,the EEROM(Electically Erasable ROM) offers
  186.         another alternative to programmable ROM.These chips function
  187.         like RAM,except that information is not lost when the power
  188.         is turned off.
  189.       WOM;
  190.         With the birth of the Amiga,another type of memory,WOM,was
  191.         created.This particular type of memory is Write Once Memory.
  192.         The Kickstart disk is read into this memory when the computor
  193.         is first booted.After this,no more data can be read into that
  194.         memory.Actually this isn't a completely new component,but
  195.         simply RAM that is locked once data has been read into it,
  196.         after which the data can only be read from that memory.
  197.       Registers;
  198.         In addition to RAM and these variations of ROM there is another
  199.         type of memory situated between those two groups.This memory
  200.         is connected to the processor through a group of peripheral
  201.         controllers.Thus it is commonly refered to as the Hardware
  202.         Register,since the computor's hardware is managed by this 
  203.         system.We'll go into greater detail on how to use these hardware
  204.         registers later in this book.
  205.         Lets take a closer look at the structure and use of the memory
  206.         most familiar to us,RAM.
  207.  
  208.       1.2.2.Bits,Bytes,and Words.
  209.       --------------------------
  210.       Kilobyte;
  211.         The standard size in which memory is measured is a Kilobyte
  212.         (Kbyte).One kilobyte consists of 1024 bytes,not 1000 as you 
  213.         might expect.This unusual system stems from the computor's
  214.         binary mode of operation,where numbers are given as powers of
  215.         2,including kilobytes.
  216.         To access a memory block of one kilobyte,the processor requires
  217.         10 connections which carry either one volt or zero volts.Thus
  218.         2^10=1024 combinations or 1024 bytes of memory,are possible.
  219.       Byte;
  220.         A byte,in turn,consists of yes/no,on/off information as well.
  221.         A byte can be one of 2^8 different values,and thus it can
  222.         represent any one of 256 numbers.The individual numerical
  223.         values that make up a byte,which also are the smallest and
  224.         most basic unit encountered in any computor,are called bits
  225.         (short for binary coded digit).
  226.         A 512 kbyte memory,such as the Amiga's,contains 2^19=524288
  227.         bytes and 4194304 bits.It may seem unimaginable,but a memory
  228.         of that size has 2^4194300 different combinations.
  229.       Word;
  230.         Back to the basics...bits and bytes are sufficent to program
  231.         an eight bit processor like the 6500,since it can only work
  232.         with bytes.To program a 16/32 bit processor like the Amiga's
  233.         MC68000,you'll need to know two new data forms:words,consisting
  234.         of 16 bits(the equivalent of two bytes),and long words,which
  235.         are 32 bits(the equivalent of four bytes,or 2 words).
  236.         A word can be any number between 0 and 65536,a long word can
  237.         0 to 4294967295.The MC68000 processor can process these
  238.         gigantic numbers with a single operation.
  239.         Once in a while you need to use negative numbers as well as
  240.         positive ones.Since a bit can only be 1 or 0 and not -1,an
  241.         alternative system has been adopted.If a word is to have a
  242.         specific sign,the highest value digit or 15th bit in the word
  243.         (positions are always counted from zero) determines the sign
  244.         of the word.With this method words can carry values from -32768
  245.         to +32768.One byte can range from -127 to +127.In a byte,the
  246.         value -1 is given by $FF; in a word it's $FFFF,-2 is $FE(FFFE),
  247.         etc.
  248.         Lets stick with positive values for the time being,to aid in
  249.         the visualization of a bit in relation to its bit pattern.
  250.         Machine language does not use the familiar decimal system.
  251.         Instead,it commonly employs the binary as well as the octal and
  252.         hexadecimal number systems.
  253.  
  254.       1.2.3.Number Systems.
  255.       --------------------
  256.         Lets take a look at the decimal system:its base number is 10.
  257.         This means that every digit represents a power of 10.This means
  258.         that the 246 represents 2*10^2+4*10^1+6*10^0.The decimal system
  259.         offers a selection of 10 characters,namely 0-9.
  260.       Binary;
  261.         This procedure is different for the binary system.The binary
  262.         system offers only two different characters:1 and 0.Thus the
  263.         systems base number is two.The decimal value of 1010 would be:
  264.           1*2^3+0*2^2+1*2^1+0*2^0=2^3+2^1=8+2=10 (in decimal system)
  265.         Generally binary numbers are identified by having a percentage
  266.         symbol as a prefix.See if you can determine the decimal value of
  267.         this number:110010...
  268.         Well did you get 50?.Thats the right answer.The most simple 
  269.         method to arrive at this result is to simply add up the values
  270.         of the digits contained at 1.The values of the first eight digits
  271.         are as follows:
  272.             digit       8     7     6     5     4     3     2     1
  273.             value     128    64    32    16     8     4     2     1
  274.       Octal;
  275.         The octal system whose base is eight,is similar.The character set
  276.         consists of numbers 0 to 7.The decimal equivalent of the octal
  277.         number 31 is: 3*8^1+1*8^0=25.However the octal system isn't 
  278.         nearly as important as the next one...
  279.         The base number of the hexadecimal system is 16,and its character
  280.         set ranges from 0 to F.Thus,A would be equivalent of a decimal 10
  281.         and F would be 15.The dollar sign($) indicates a hexadecimal
  282.         number.The binary and hexadecimal systems are the most important
  283.         numerical systems for assembly language programming.
  284.       Hex;
  285.         The hexadecimal representation of a byte ranging from 0 to 256
  286.         always has two digits:$00 to $FF.A word ranges from $0000 to $FFFF
  287.         and a longword from $00000000 to $FFFFFFFF.
  288.         Its quite easy to convert binary numbers into hexadecimal:simply
  289.         split up the binary numbers into groups of four digits.Each of 
  290.         these groups of four digits then corresponds to one hexadecimal
  291.         digit.Heres an example:
  292.                 binary number  %110011101111
  293.                 split up       %1100 %1110 %1111
  294.                 result           $C    $E    $F
  295.                 thus:          %110011101111=$CEF 
  296.         The opposite operation is just as easy...
  297.                 hexadecimal   $E30D
  298.                 split up      $E     $3     $0     $D
  299.                 result        %1110  %0011  %0000  %1101
  300.                 thus:         $E30D = %1110001100001101
  301.         This method can also be used to convert binary into octal and vice
  302.         versa,except that groups of three digits are used in that case:
  303.                 octal number   7531
  304.                 split up       7     5     3     1
  305.                 result         %111  %100  %011  %001
  306.                 thus:          octal 7531=%111101011001
  307.         This binary number can the be converted into hexadecimal,as well:
  308.                 binary number  %111101011001
  309.                 split up       %1111 %0101 %1001
  310.                 result         $F    $5    $9
  311.                 thus:          octal 7531=$F59
  312.         The following calculation can then be used to convert the number
  313.         into the familiar decimal system:
  314.                 hexadecimal    $F59
  315.                 split up       $F     $5     $9
  316.                 result         15*16^2+5*16+9
  317.                 thus:          $F59=3929 decimal
  318.         Although this conversions are quite simple ,they can get to be
  319.         rather annoying.Many assemblers can ease this task somewhat:they
  320.         allow you to enter a value with '?'upon which it returns the
  321.         value in decimal and hexadecimal forms.There are even calculators
  322.         that perform number base conversions.
  323.         Often this conversion as to be performed in a program,for instance
  324.         when a number is entered by the user and then processed by the
  325.         computor.In this case the number entered,being simply a
  326.         combination of graphic symbols,is evaluated and the usually
  327.         converted into a binary number,in effect,a word or a longword.
  328.         This process is often required in reverse order,as well.If the
  329.         computor is to display a calculated value in a specific number
  330.         system,it must first convert that number into a series of
  331.         characters.In a later chapter you will develop machine language
  332.         routines to solve these problems.You can then use these routines
  333.         in your own programs.First you still have to cover some things
  334.         that are fundamental to machine language programming on the Amiga.
  335.  
  336.       1.3.Inside the Amiga.
  337.       --------------------  
  338.         In order to program machine language,it is not sufficent to know
  339.         only the commands of the particular processor,one must also have
  340.         extensive knowledge of the Amiga being programmed.Lets take a
  341.         look inside the Amiga.
  342.  
  343.       1.3.1.Components and Libraries.
  344.       ------------------------------
  345.         The Amiga is a very capable machine,due to the fact that there
  346.         are components that do a large part of the workload,freeing up
  347.         the 68000 processor.These are refered to as the"custom"chips,
  348.         which perform various tasks independantly of the 68000 processor.
  349.       Custom Chips;
  350.         This task force is comprised of three chips,whose poetic names
  351.         are Agnus,Denice,and Paula.The main task of Agnus,alias blitter,
  352.         is the shifting of memory blocks,which is helpful for operations
  353.         such as quick screen changes.Denise is responsible for transfering
  354.         the computors thoughts on to the screen.Paula's tasks consist of
  355.         input/output jobs,such as disk operation or sound.
  356.         These chips are accessed by the processor through several adresses
  357.         starting at $DFF000,which are also known as the hardware registers
  358.         (you'll find more detailed information about the registers in the
  359.         corresponding chapter).To simplify the otherwise complicated
  360.         procedure of utilizing these chips,several programs have been
  361.         included in the Kickstart and Workbench libraries.These programs
  362.         can be called by simple routines and then take over the respective
  363.         chips.
  364.         If only these library functions are used to program the Amiga,the
  365.         parameters are the same,regardless of the language used.Only the
  366.         parameter notations differs from language to language.BASIC is an
  367.         exception in this respect,since its interpreter translates the
  368.         program calls,which is why you don't need to know how the Amiga
  369.         executes these functions in order to use them.
  370.         The library functions are written in machine language and are thus
  371.         closely related with your own machine language programs.Actually
  372.         you could do without the library programs and write all of the
  373.         functions yourself.However the incredible workload of this task is
  374.         so discouraging,that you'd rather stick with the library functions
  375.  
  376.       1.3.2.Memory. 
  377.       ------------   
  378.         First lets look at the RAM of the Amiga 1000.The standard version
  379.         of this computor has over 512 kbytes of RAM,ranging from the
  380.         address $00000 to $7FFFF,or 0 to 524287.If the memory is expanded
  381.         to one megabyte,the first address still starts at $00000,however
  382.         the start of anything greater than 512k can go anywhere in the
  383.         address space between $200000 to $9FFFFF.With the release of
  384.         AmigaDOS 1.2,the Amiga figures out where to put the memory 
  385.         expansion by using a special`Autoconfig`scheme.This allows you to
  386.         add memory and I/O without worrying about addresses and dip
  387.         switches.
  388.       Chip RAM;
  389.         The chips that support the Amiga`s processor access RAM almost
  390.         totally independantly and thus ease the workload of the processor.
  391.         However there is a draw back:these chips can only access the first
  392.         512k bytes of RAM.Thus graphics and sound data handled by these
  393.         chips MUST be stored in this memory range.Because of this,that
  394.         memory range is referred to as `Chip RAM`.
  395.       Fast RAM;
  396.         The counterpart to chip RAM is the remaining RAM which,if the
  397.         computor is equipped with it,begins at $200000.Since only the
  398.         processor itself as access to this part of memory it is known has
  399.         `Fast RAM`.
  400.         Here`s an overview of the Amiga`s memory:
  401.                 $000000-$07FFFF      chip RAM
  402.                 $080000-$1FFFFF      reserved
  403.                 $200000-$9FFFFF      potential fast RAM
  404.                 $A00000-$BEFFFF      reserved
  405.                 $BFD000-$BFDF00      PIA B (even addresses)
  406.                 $BFE001-$BFEF00      PIA C (odd addresses)
  407.                 $C00000-$DFEFFF      reserved for expansion
  408.                 $DFF000-$DFFFFF      custom chip registers
  409.                 $E00000-$E7FFFF      reserved
  410.                 $E80000-$EFFFFF      expansion ports
  411.                 $F00000-$F7FFFF      reserved
  412.                 $F80000-$FFFFFF      system ROM
  413.         Since the Amiga is multi-tasking,when a program is loaded into
  414.         memory,it is simply loaded into another memory location.The memory
  415.         range thus occupied is added to a list of occupied memory and the
  416.         memory range is then considered barred from other uses.If another
  417.         program is loaded,which is quite possible with the Amiga,it is
  418.         read into another memory location which is then marked on the 
  419.         occupied list.If the first program should require additional
  420.         memory,to use a text buffer for example,that memory first has to
  421.         be reserved.Otherwise another program could accidently be loaded
  422.         into the memory needed for this task.
  423.         What`s interesting about this procedure is that when the first
  424.         loaded has ended,the memory occupied by it is freed for further
  425.         use.As a result,RAM is then chopped up into occupied and free
  426.         parts,which are no longer related to each other.The Amiga can
  427.         still utilize these chunks of memory as if they were one
  428.         continuous chunk.After all,parts is parts.An example of this is
  429.         the dynamic RAM disk which is always available under the name RAM:
  430.         This RAM disk is actually quite a phenomenon,since it is always
  431.         completely filled.If a program is erased from RAM disk,the memory
  432.         allocated to that program,regardless of its location or structure,
  433.         is given back to the system.Thus,if you reserved and filled 100
  434.         kbytes of memory,it would be quite posible that the 100kbytes
  435.         actually consists of various pieces of memory independant of one
  436.         another.You never notice this since the Amiga automatically
  437.         corrects the difference between apparent and actual memory.
  438.  
  439.       1.3.3.Multi-Tasking.
  440.       -------------------
  441.         The Amiga is truly an amazing machine,being capable of doing
  442.         several things at one time.A red and white ball might be bouncing
  443.         around in one window while you`re working on text in another
  444.         window and watching a clock tick away in a third.
  445.         At least that`s the impression most people get when they recieve
  446.         their first Amiga demonstration.However,there is a catch to this:
  447.         even the Amiga as only one processor,which can really only do one
  448.         thing at a time.
  449.         The tricky part is when more than one program is running,each
  450.         program is executed part by part,and the Amiga is constantly
  451.         switching from one program back to the other program.In the
  452.         example above,the ball would first be moved by one pixel,then
  453.         the processor would check for a text entry and if necessary display
  454.         it,after which it would move the clock`s second hand.This
  455.         procedure would be repeated over and over,as the three programs
  456.         are executed together.The problem is,that the greater the work
  457.         load on the processor,the slower the things happen.Thus,programs
  458.         run slower during heavy multi-tasking.
  459.       Tasks;
  460.         Each of these jobs that the Amiga has to execute are commonly
  461.         referred to has tasks...thus,multi-tasking.During multi-tasking,
  462.         each task is assigned a special time segment during which that
  463.         particular task is executed.These time segments can be controlled,
  464.         so that more time consumming programs can be allotted somewhat
  465.         more processing time.
  466.         The programmer actually doesn`t need to know how this time slicing
  467.         works.You can write aprogram without paying any attension to
  468.         multi-tasking and then run it simultaneously with another program
  469.         running in the background.The only restriction is that you`ll have
  470.         to start the program from the CLI with`run`,or from the Workbench.
  471.         If you execute the program from the CLI by simply typing its name,
  472.         the processor allots all the time it can get from the CLI to that
  473.         program,until the execution is complete.Starting the program with
  474.         run free`s the CLI for other uses while the program is being
  475.         executed.
  476.         There is another restriction regarding multi-tasking that applies
  477.         to assembler programmers.Aside from the use of extra memory,which
  478.         must first be reserved,the hardware registers should not be
  479.         directly accessed.Instead the library functions should be used.The
  480.         reason for this is quite simple:
  481.         Should you,for instance,specify the printer port as the input line
  482.         and are reading data in,another task might suddenly think its
  483.         supposed to be printing.The line would thus be switched to output
  484.         and data would be written out.After this,your program would try to
  485.         read more data in,which would not be possible.
  486.         This is an oversimplified example,but it points out the problem
  487.         nevertheless.In real programming situations the effects of
  488.         multiple direct programming of the hardware registers can be much
  489.         more catastrophic.If your program still needs to access the
  490.         hardware registers directly(which can have some advantages),then
  491.         make sure that the program always runs by itself.
  492.  
  493.         
  494.  
  495.  
  496.       Chapter 2.
  497.       ---------
  498.       2.The MC68000 Processor.
  499.       -----------------------
  500.         The Amiga`s MC68000 processor is a 16/32 bit processor,which means
  501.         that while it can process data of 32 bits,it"only"has a 16 bit
  502.         data bus and a 24 bit address bus.Thus,it can access 2^24=16777216
  503.         bytes(or 16 Mbytes)of memory directly.
  504.       7.1 Megaherz;
  505.         The Amiga 68000 processor,running at 7.1 megaherz,is quite fast,
  506.         which is required for a computor with a workload as heavy as the
  507.         Amiga`s.The Amiga also processes a number of custom chips that
  508.         greatly ease the workload of the processor.These custom chips
  509.         manage sound in/output,graphics and animation,thus freeing the
  510.         processor for calculations.
  511.         
  512.       2.1.Registers.
  513.       -------------
  514.         In addition to the standard RAM,the processor contains internal
  515.         memory called registers.There are eight data registers(D0-D7),
  516.         eight address registers(A0-A7),a status register(SR),two stack
  517.         pointers,a user stack pointer,a system stack pointer(USP and SSP)
  518.         and the program counter(PC).
  519.       Register Sizes;
  520.         The data registers,the address registers,and the program counter
  521.         are all 32 bits,while the status register is 16 bits.These
  522.         registers are located dirctly in the processor so they are not 
  523.         accessed the same way memory would be accessed.There are special
  524.         instructions for accessing these registers.
  525.       Data Registers;
  526.         The data registers are used for all kinds of data.They can handle
  527.         operations with bytes(8 bits)words(16 bits)and longwords(32bits).
  528.       Address Registers;
  529.         The address registers are used for storing and processing
  530.         addresses.This way they can be used as pointers to tables,in which
  531.         case only words and longwords operations are possible.
  532.       Stack Pointer;
  533.         The address register A7 plays a special role:this register is
  534.         utilized as the Stack Pointer(SR)by the processor,and thus is not
  535.         recommended for normal use by the programmer.Which of the two
  536.         possible stacks is being pointed to depends on the present mode of
  537.         the processor,but more about that later.
  538.         The stack,to whose actual position the stack pointer is pointing,
  539.         is used to store temporary internal data.The stack works similar 
  540.         to a stack of notes on your desk:the note that was added to the
  541.         stack last is the first one to come off the stack.This type of
  542.         stack is known as LIFO(Last in,First out).There is another type of
  543.         stack,the FIFO(First in,First out)which is not used by the
  544.         processor itself.
  545.         How these registers and the SP can be manipulated,or how to work
  546.         with the stack,is presented in the next chapter.Lets continue with
  547.         the registers for now.
  548.       Status Register;
  549.         The status register plays an important role in machine language
  550.         programming.This 16-bit quality(word)contains important
  551.         information about the processor status in 10 of its bits.The word
  552.         is divided into two bytes,the lower byte(the user byte)and the
  553.         upper byte(the system byte).The bits that signify that certain
  554.         conditions are refered to as flags.This means that when a certain
  555.         condition is present,a particular bit is set.
  556.         The user byte contains five flags,which have the following meaning
  557.  
  558.                 Bit     Name           Meaning
  559.                 -----------------------------------------------
  560.                  0      (C,Carry)      Carry bit,modified by math
  561.                                        calculation,and shift instructions.
  562.                  1      (V,Overflow)   Similar to carry,indicates a change
  563.                                        of sign,in other words,a carry from
  564.                                        bit six to bit seven.
  565.                  2      (Z,Zero)       Bit is set when the result of an
  566.                                        operation is zero.
  567.                  3      (N,Negative)   Is set when the result of an
  568.                                        operation  is negative.
  569.                  4      (X,Extended)   Like carry,is set for arithmetic
  570.                                        operations.
  571.                  5-7                   Not used.
  572.  
  573.         The system byte contains five significant bits:
  574.  
  575.                 Bit     Nane           Meaning
  576.                 -----------------------------------------------
  577.                  8      I0             Interupt mask.Activates interupt
  578.                  9      I1             levels 0 to 7,where 0 is the lowest
  579.                  10     I2             and 7 is the highest priority.
  580.                  11                    not used.
  581.                  12                    not used.
  582.                  13    (S,Supervisor)  This bit indicates the actual
  583.                                        pocessor mode(0=User,1=Supervisor
  584.                                        mode).
  585.                  14                    not used.
  586.                  15    (T,Trace)       If this bit is set,the processor is
  587.                                        in single step mode.
  588.  
  589.         Here's an overview of the status word;
  590.                  
  591.                  bit  : 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0
  592.                  name :  T  -  S  -  - I2 I1 I0  -  -  -  X  N  Z  V  C
  593.         
  594.         Don't let the new terms,like mode and interupt confuse you.We'll
  595.         talk about these in greater detail in the chapter dealing with the
  596.         operating conditions of the processor.
  597.  
  598.       2.2.Addressing Memory.
  599.       ---------------------
  600.         In the standard Amiga 500's and 1000's,the processor has over 512k
  601.         of RAM available.The Amiga 2000 has one mega-byte of RAM that can
  602.         be accessed by the processor.How does the processor access all
  603.         this memory?
  604.         If you're programming in BASIC you don't have to worry about
  605.         memory management.You can simply enter MARKER%=1,and the value is
  606.         stored in memory by the BASIC interpreter.
  607.         In assembler,there are two ways of accomplishing this task:
  608.            1) Store the value in one of the data or address registers,or
  609.            2)Write it directly into a memory location.
  610.         To demonstrate these two methods let's get a little ahead and
  611.         introduce a machine language instruction,which is probably the
  612.         most common:MOVE.As its name states,this instruction moves values.
  613.         Two parameters are required with the instruction:source and
  614.         destination.
  615.         Lets see what the example from above would look like if you
  616.         utilize the MOVE instruction...
  617.            1)  MOVE   #1,D0
  618.         This instruction moves the value 1 into data register D0.As you
  619.         can see,the source value is always entered before the destination.
  620.         Thus,the instruction MOVE D0,#1 is not possible.
  621.            2)  MOVE   #1,$1000
  622.         deposits the value 1 in the memory location at $1000.This address
  623.         was arbitrarily chosen.Usually addresses of this form won't be
  624.         used at all in assembler programs,since labels that point to a 
  625.         certain address are used instead.Thus,the more common way of
  626.         writing this would be:
  627.              ...
  628.              MOVE  #1,MARKER
  629.  
  630.              ...
  631.           MARKER:DC.W 1
  632.  
  633.         These are actually two pieces of program:the first part executes 
  634.         the normal MOVE instruction whose destination is `MARKER`.This
  635.         label is usually defined at the end of a program and specifies the
  636.         address at which the value is stored.
  637.         The paraneter DC.W 1 is a pseudo op,apseudo operation.This means
  638.         that this isn`t an instruction for the processor,but an
  639.         instruction for the assembler.The letters DC stand for`DeClare`and
  640.         the suffix .W indicates that the data is a Word.The other two
  641.         suffix alternatives would be .B for a byte(8 bits)and .L for a
  642.         long word(32 bits).
  643.         This suffix(.B.W or.L)is used with most machine language
  644.         instructions.If the suffix is omitted,the assembler uses .W(word)
  645.         as the default parameter.If you wanted a long word,you`d use an
  646.         instruction that looks something like this:MOVE .L #$1234678,D0
  647.         where as an instruction like MOVE.B #$12,D0 would be used for a
  648.         byte of data.However,with this instruction there`s one thing you
  649.         must be aware of...
  650.         CAUTION:
  651.         If the memory is accessed by words or long words,the address must
  652.         be even(end digit must be 0,2,4,6,8,A,C,E)!
  653.         Assemblers usually have a pseudo-op,`EVEN`or`ALIGN`,depending on
  654.         the assembler,that aligns data to an even address.This becomes
  655.         necessary in situations similar to this:
  656.              ...
  657.           VALUE1:    DC.B 1
  658.           VALUE2:    DC.W 1
  659.  
  660.         If the VALUE1 is located at an even address,VALUE2 is automaticaly
  661.         located at an odd one.If an ALIGN(EVEN)is inserted here,a fill
  662.         byte(0)is inserted by the assembler,thus making the second address
  663.         even.
  664.              ...
  665.           VALUE1:    DC.B 1
  666.           ALIGN
  667.           VALUE2:    DC.W 1
  668.  
  669.         Back to the different ways of addressing.The variations listed
  670.         above are equivalent to the BASIC instruction MARKER%=1 where the
  671.         % symbol indicates an integer value.
  672.         Lets go a step further and translate the BASIC instruction MARKER
  673.         %=VALUE% into assembler.You`ve probably already guessed the answer
  674.         right?
  675.  
  676.                  MOVE VALUE,MARKER
  677.                  ...
  678.                  ...
  679.           MARKER:      DC.W 1
  680.           VALUE :      DC.W 1
  681.  
  682.         In this case,the contents in the address at VALUE are moved into
  683.         the address at MARKER.
  684.         With the help of these simple examples,you`ve already become
  685.         familiar with four different ways of addressing,in other words,
  686.         ways that the processor can access memory.The first is
  687.         characterized by the number sign(#)and represents a direct value.
  688.         Thus,this method is also known as direct addressing,and is legal
  689.         only for the source parameter!
  690.         A further method in which a direct address(in our case,`MARKER`and
  691.         `VALUE`)can be specified is known as absolute addressing.This
  692.         method is legal for the source parameter as well as for the
  693.         destination parameter.
  694.         This method can be divided into two different types,between which
  695.         the programmer usually does'nt notice a difference.Depending on
  696.         whether the absolute address is smaller or larger than $FFFF,in
  697.         other words if it requires a long word,it is called absolute
  698.         addressing(for addresses above $FFFF)or otherwise absolute short
  699.         addressing.The assembler generally makes the distinction between
  700.         these two types,and thus,only general knowledge of absolute
  701.         addressing is required.
  702.         The fourth method of addressing that you've encountered so far is
  703.         known as DATA REGISTER DIRECT.It was the first one introduced(MOVE
  704.         #1,D0)in conjunction with direct addressing,the only difference
  705.         being that this type accesses a data register(such as D0).
  706.         These four methods aren't the only ones available to the MC68000
  707.         processor,in fact there are a total of 12.One other variation
  708.         called ADDRESS REGISTER DIRECT,is almost identical to data
  709.         register direct,except that it accesses the address register
  710.         instead of the data register.Thus,you can use MOVE.L #MARKER,A0 to
  711.         access the address register A0 directly.
  712.         You now know five ways to address memory with which quite a bit
  713.         can be accomplished.Now,lets tackle something more complicated and
  714.         more interesting.
  715.         Lets take another example from BASIC:
  716.             
  717.             10  A=1000
  718.             20  POKE A,1
  719.  
  720.         In this example the first line assigns the value 1000 to the
  721.         variable A.This can be done in assembler as well:MOVE.L #1000,A0.
  722.         In the assembler version the absolute value of 1000 ia stored in
  723.         the address register A0.
  724.         Line 20 doesn't assign a value to the variable A itself,but rather
  725.         to the memory location at the address stored in A.This is an
  726.         indirect access,which is quite easy to duplicate in assembler:
  727.              
  728.             MOVE.L  #1000,A0       ;bring address in A0
  729.             MOVE    #1,(A0)        ;write 1 into this address
  730.  
  731.         The parentheses indicates an addressing known as ADDRESS REGISTER
  732.         INDIRECT.This method only works with address registers,since a
  733.         'data register indirect' does not exist.
  734.         There are several variations of this method.For instance,a
  735.         distance value can be specified,which is added to the address
  736.         presently located in the address register before the register is
  737.         actually accessed.The instruction MOVE #1,4(A0),if applied to the
  738.         example above,writes the value 1 into the memory cell at 1000+4=
  739.         1004.This distance value can be positive or negative.Thus,values
  740.         from -32768 to +32768 are accepted.This specific variation of
  741.         addressing is called ADDRESS REGISTER INDIRECT WITH A 16 BIT
  742.         DISPLACEMENT value.
  743.         There is another very similar variation:ADDRESS REGISTER INDIRECT
  744.         WITH AN 8 BIT INDEX value.While this variation is limited to 8
  745.         bits,it also brings another register into play.This second
  746.         register is also a distance value,except that it is a variable as
  747.         well.
  748.         We'll try to clarify this with an example.Lets assume that a
  749.         program includes a data list that is structured like this:
  750.            
  751.                ...
  752.             RECORD:         DC.W 2   ;number of entries -1
  753.                    DC.W 1,2,3        ;elements of list
  754.         
  755.         We'll use MOVE.L #RECORD,A0 to load the list into the address
  756.         register A0.Then you can use MOVE (A0),D0 to pull the number of
  757.         in the list into the data register.To access the last element of
  758.         the listonly one instruction is needed.The whole thing looks
  759.         something like this:
  760.  
  761.                    CLR.L   D0            ;erase D0 completely
  762.                    MOVE.L  #RECORD,A0    ;address of list in A0
  763.                    MOVE    (A0),D0       ;number of elements -1 in D0
  764.                    MOVE    1(A0,D0),D1   ;last element in D1
  765.                    ...
  766.            RECORD: DC.W 2                ;number of entries -1
  767.                   DC.W 1,2,3             ;elements of list
  768.  
  769.         This last instruction accesses the byte that is located at 1+A0+D0
  770.         in other words,the record +1 where the data begins plus the
  771.         contents of D0(in this case 2).
  772.         This method of accessing is very useful.It works exquisitely for
  773.         the processing of tables and lists,as the example demonstrates.If 
  774.         no distance value is needed,simply use a distance value of zero,
  775.         which some assemblers automatically insert as the default value if
  776.         for instance only MOVE (A0,D0)is entered.
  777.         The latter two methods have a third variation,which as its own
  778.         characteristic trait.It dosen't utilize an address register,but
  779.         uses the Program Counter(PC)instead.The program counter with 
  780.         displacement method proves useful when a program must function 
  781.         without any changes in all address ranges.The following two
  782.         statements(in the 15 bit limits)have the same effect:
  783.  
  784.                    MOVE  MARKER,D0
  785.  
  786.            and
  787.  
  788.                    MOVE  MARKER(PC),D0
  789.  
  790.         This method is actually rather imprecise,since the first
  791.         instruction specifies the actual address of the marker with MARKER
  792.         while the second line specifies the distance between the
  793.         instruction and the marker.However since it would be quite
  794.         cumbersome to constanly calculate the distance,the assembler takes
  795.         this task off our hands and calculates the actual value automatic.
  796.         Lets examine the difference between the two instructions.In a
  797.         program they'll accomplish the same thing,although they are
  798.         interpreted as two completely different things by the assembler.
  799.         You'll assume a program being at the address $1000 and the marker
  800.         is located at $1100.The generated program code looks something
  801.         like this:
  802.  
  803.            $001000      30 39 00 00 11 00     MOVE  MARKER,D1
  804.  
  805.           or
  806.  
  807.            $001000      30 3A 00 FE           MOVE  MARKER(PC),D1
  808.  
  809.         As you can see,the generated code of the second line is two bytes
  810.         shorter than the first line.In addition,if you were to shift this
  811.         code to the address $2000,the first version still accesses the
  812.         memory at $1100,while the second line using the PC indirect
  813.         addressing accesses memory at $2000 correctly.Thus,the program can
  814.         be transferred to almost any location.
  815.         This,then,is PROGRAM COUNTER WITH 16 BIT DISPACEMENT value.As we
  816.         mentioned,there is also PROGRAM COUNTER WITH 8 BIT INDEX value,
  817.         which permits a second register as a distance value,also known as
  818.         offset.
  819.         There are two addressing modes left.These two are based on
  820.         indirect addressing.They offer the capability of automatically
  821.         raising or lowering the address register by one when memory is
  822.         accessed with address register indirect.
  823.         To automatically increase the register,you'd use ADDRESS REGISTER
  824.         DIRECT WITH POST-INCREMENT.The address register raises this by the
  825.         number of bytes used AFTER accessing memory.Thus if you write:
  826.  
  827.              MOVE.L  #1000,A0
  828.              MOVE.B  #1,(A0)+
  829.  
  830.         the 1 is written in the address $1000 and then A0 is raised by one
  831.         Instructions like this are very helpful when a memory range is to
  832.         be filled with a specific value(for instance when the screen is
  833.         cleared).For such purposes the instruction can be placed in a loop
  834.         ...which we'll get to later.
  835.         The counter part to post increment is ADDRESS REGISTER WITH PRE-
  836.         DECREMENT.In this case the specified address register is lowered
  837.         by one BEFORE the access to memory.The instructions:
  838.  
  839.              MOVE.L  #1000,A0
  840.              MOVE.B  #1,-(A0)
  841.  
  842.         writes 1 in the address 999,since the contents of A0 is first 
  843.         decremented and the 1 is written afterwards.
  844.         These two methods of addressing are used to manage the stack
  845.         pointer(SP).Since the stack is filled from top to bottom,the
  846.         following is written to place a word(s.aD0)on the stack:
  847.  
  848.              MOVE.B  D0,-(SP)
  849.  
  850.         and to remove it from the stack,again in D0:
  851.  
  852.              MOVE.B  (SP)+,D0
  853.  
  854.         This way the stack pointer always points to the byte last
  855.         deposited on the stack.Here,again,you'll have to be careful that
  856.         an access to the stack with a word or a long word is always on an
  857.         even address.Thus,if you're going to deposit a byte on the stack,
  858.         either use a whole word or make sure the byte is not followed by a
  859.         JSR or BSR.The JSR or BSR instructions deposit the addresses from
  860.         which they stem on the stack in the form of a long word.
  861.         In the code above,the SP is generally replaced by the address
  862.         register A7 in the program code,since this register is always used
  863.         as the SP.Thus,you can write A7 as well as S,the resulting program
  864.         is the same.However,we recommend the use of SP,since this makes
  865.         the code somewhat easier to read.After all,quite often you'll want
  866.         to employ your own stacks,in which case the difference between the
  867.         processor stack and your own stacks become very important.
  868.         These are the twelve ways of addressing the MC68000 processor,Here
  869.         is a summary:
  870.  
  871.         No  Name                                                Format
  872.         --  ----                                                ------
  873.          1  data register direct                                Dn
  874.          2  address register direct                             An
  875.          3  address register indirect                           (An)
  876.          4  address register indirect with post-increment       (An)+
  877.          5  address register indirect with pre-decrement        -(An)
  878.          6  address register indirect with 16 bit displacement  d16(An)
  879.          7  address register indirect with 8 bit index value    8(An,Rn)
  880.          8  absolute short                                      xxxx.W
  881.          9  absolute long                                       xxxxxxxx.L
  882.         10  direct                                              #'data'
  883.         11  program counter indirect with 16 bit displacement   d16(PC)
  884.         12  program counter indirect with 8 bit index value     d8(PC,Rn)
  885.  
  886.         The abbreviations above have the following meanings:
  887.  
  888.              An      address registers A0-A7
  889.              Dn      data registers D0-D7
  890.              d16     16 bit value
  891.              d8      8 bit value
  892.              Rn      register D0-D7,A0-A7
  893.              'data'  up to 32 bit value,(either .B .W .L)
  894.  
  895.         These are the addressing modes used by the MC68000 processor.The
  896.         bigger brother of this processor,the 32 bit MC68020,has six more
  897.         methods which we won't discuss here.
  898.         Next you're going to see under what conditions the processor can
  899.         operate.
  900.  
  901.       2.3.Operating Modes.
  902.       -------------------
  903.         In the previous section about registers you encountered the Status
  904.         Register(SR).The individual bits of this register reflect the
  905.         present operating condition of the processor.You differentiated
  906.         between the system byte(bits 8-15)and the user byte(bits 0-7).Now,
  907.         lets take a closer look at the system byte and its effects upon
  908.         the operation of the processor.
  909.  
  910.       2.3.1.User and Supervisor Modes.
  911.       -------------------------------
  912.         Isn't it rather strange that the processor classifies you either
  913.         as a'user'or a 'supervisor'?Both of these operating modes are
  914.         possible,the user mode being the more common mode.In this mode it
  915.         is impossible to issue some instructions,and that in your own
  916.         computor!
  917.         Don't worry though,you can get around that,as well.The Amiga's
  918.         operating system contains a function that allows us to switch the
  919.         processor from one mode to the other.
  920.         The mode is determined by bit 13 of the Status Register.Usually
  921.         this bit is cleared(0),indicating that the processor is in user
  922.         mode.It is possible to write directly into the status register,
  923.         although this is a privileged instruction that can only be
  924.         executed from the supervisor mode.Thus,this instructioncould only
  925.         be used to switch from the supervisor mode into the user mode,by 
  926.         using AND #$DFFF,SR to clear the supervisor bit.However,it is
  927.         quite preferable to let the operating system perform the switch
  928.         between these two modes.
  929.         Now what differentiates these two modes in terms of application?
  930.         Well,we already mentioned the first difference:some instructions,
  931.         such as MOVE xx,SR, are privileged and can only be executed from
  932.         the supervisor mode.An attempt to do this in user mode would
  933.         result in an exception and interruption of the program.Exceptions
  934.         are the only way of switching to the supervisor mode,but more
  935.         about later.
  936.         A further difference is in the stack range used.Although A7 is
  937.         still used as the stack pointer,another memory range is used for
  938.         the stack itself.Thus,the SP is changed rach time you switch from
  939.         one mode to the other.Because of this you differentiate between
  940.         the User(USP)and the Supervisor SP(SSP).
  941.         Accessing memory can also depend on these two modes.During such
  942.         accessing,the processor sends signals to the peripheral components
  943.         informing them of the current processor moce.This way a MC68000 
  944.         computor can protect(privilege)certain memory ranges so they can't
  945.         be accessed by the user.
  946.         In the supervisor mode it is possible to execute all instructions
  947.         and access all areas of memory.Because of this,operating systems
  948.         usually run in the supervisor mode.This is accomplished through
  949.         the use of Exceptions.
  950.  
  951.       2.3.2.Exceptions.
  952.       ----------------
  953.         Exceptions are similar to interupts on 6500 computors.This allows
  954.         stopping a program,running a sub-program,and then restarting the
  955.         stopped program.When an exception occurs the following seps are
  956.         taken:
  957.  
  958.            1)   The status register is saved.
  959.            2)   The S bit in SR is set(supervisor mode)and the T bit is
  960.                 cleared(no trace).
  961.            3)   The program counter and the user SP are saved.
  962.            4)   The exception vector,which points to the needed exception
  963.                 routine,is retrieved.
  964.            5)   The routine is executed.
  965.  
  966.         The vectors mentioned,which contain the starting addresses for the
  967.         various routines,are located at the very beginning of the memory.
  968.         Here is an overview of the vectors and their respective addresses:
  969.  
  970.            Number   Address       Used for
  971.            -----    ------        --------
  972.             0       $000          RESET:starting SSP
  973.             1       $004          RESET:starting PC
  974.             2       $008          bus error
  975.             3       $00C          address error
  976.             4       $010          illegal instruction
  977.             5       $014          division by zero
  978.             6       $018          CHK instruction
  979.             7       $01C          TRAPV instruction
  980.             8       $020          privilege violation
  981.             9       $024          trace
  982.            10       $028          Axxx-instruction emulation
  983.            11       $02C          Fxxx-instruction emulation
  984.                     $030-$038     reserved
  985.            15       $03C          uninitialed interrupt
  986.                     $040-$05F     reserved
  987.            24       $060          unjustified interrupt
  988.            25-31    $064-$083     level 1-7 interrupt
  989.            32-47    $080-$0BF     TRAP instructions
  990.                     $0C0-$0FF     reserved
  991.            64-255   $100-$3FF     user interrupt vectors
  992.  
  993.         The individual entries in the table above need detailed
  994.         explanation.So lets go through them one by one...
  995.       RESET:staring SSP;
  996.         At reset,the long word stored at this location is used as the
  997.         stack pointer for the supervisor mode(SSP).This way you can
  998.         specify the stack for the RESET routine.
  999.       RESET:starting PC;
  1000.         Again at reset,the value at this location is used as the program
  1001.         counter.In other words,the RESET routine is started at the address
  1002.         stored here.
  1003.       Bus Error;
  1004.         This exception is activated by a co-processor when,for instance,a
  1005.         reserved or non-existant memory range is accessed.
  1006.       Address Error;
  1007.         This error occurs when a word or long word access is atempted at
  1008.         an odd address.
  1009.       Illegal Instruction;
  1010.         Since all MC68000 instructions consist of one word,a total 65536
  1011.         different instructions are possible.However,since the processor
  1012.         doesn't that many instructions,there are a number of words that
  1013.         are invalid instructions.Should such a word occur,the exception is
  1014.         prompted.
  1015.       Division by Zero;
  1016.         Since the processor has a division function,and the division of
  1017.         anything by zero is mathematically undefined and thus illegal,this
  1018.         exception occurs when such an operation is attempted.
  1019.       CHK Instruction;
  1020.         This exception only occurs with the CHK instruction.This
  1021.         instruction tests that a data registers contents are within a
  1022.         certain limit.If this is not the case,the exception is activated.
  1023.       TRAPV Instruction;
  1024.         If the TRAPV instruction is executed and the V bit(bit 1)in the
  1025.         status word is set,this exception is prompted.
  1026.       Privilege Violation;
  1027.         If a privileged instruction is called from the user mode,this
  1028.         exception is activated.
  1029.       Trace;
  1030.         If the trace bit(bit 15)in the status word is set,this exception
  1031.         is activated after each instruction that is executed.This method
  1032.         allows you to employ a step by step execution of machine programs.
  1033.       Axxx-Instruction Emulation;
  1034.       Fxxx-Instruction Emulation;
  1035.         These two vectors can be used for a quite interesting trick.If an
  1036.         instruction beginning with $A or $F(such as $A010 or $F200)is
  1037.         called the,the routine to which the corresponding vector is
  1038.         pointing is accessed.In these routines you can create chains of
  1039.         other instructions,in effect expanding the processors instruction
  1040.         vocaulary!
  1041.       Reserved;
  1042.         These vectors are not used.
  1043.       Uninitialized Interrupt;
  1044.         This exception is activated when a peripheral component that was
  1045.         not initialized sends an interrupt.
  1046.       Unassigned Interrupt;
  1047.         Is activated when a BUS error occurs during the interrupt
  1048.         verification of the activating component.However,the interrupt is
  1049.         usually only by some type of disturbance.
  1050.       Level 1-7 Interrupt;
  1051.         These seven vectors point to the interrupt routines of the
  1052.         corresponding priority levels.If the level indicated in the status
  1053.         word is higher than the level of the occuring interrupt,the
  1054.         interrupt is simply ignored.
  1055.       TRAP Instructions;
  1056.         These 16 vectors are used when a corresponding TRAP instruction
  1057.         occurs.Thus,TRAP instructions from TRAP #0 to TRAP #15 are
  1058.         possible.
  1059.       User Interrupt Vectors;
  1060.         These vectors are used for interrupts which are activated by
  1061.         several peripheral components that generate their own vector
  1062.         number.
  1063.         
  1064.         At this point you don't want to delve any deeper into the secrets
  1065.         of exceptions,since we'd be expanding this book beyond its frame
  1066.         work.However,there's one last thing to say about exceptions:the
  1067.         exception routines are ended with the RTE(ReTurn from Exception)
  1068.         instruction,with which the original status is restored and the
  1069.         user program is continued.
  1070.  
  1071.       2.3.3.Interrupts.
  1072.       ----------------
  1073.         Interrupts are processed similarly to exceptions.They are breaks
  1074.         (or interruptions)in the program which are activated through
  1075.         hardware(such as a peripherical component or an external trigger).
  1076.         The interrupt level is stored in bits 8-10 of the status register.
  1077.         A value between 0 and 7 indicates the interrupt level.There are
  1078.         eight possible interrupts,each of which as a different priority.If
  1079.         the level of this interrupt happens to be higher than the value in
  1080.         the status register,the interrupt is executed,or else ignored.
  1081.         When a valid interrupt occurs,the computor branches to the
  1082.         corresponding routine whose address is indicated in the exception
  1083.         vector table above.
  1084.         The interrupts are very important if you're trying to synchronize
  1085.         a program with connected hardware.In this manner,a trigger(s.a the
  1086.         keyboard)which is to feed the computor data,can signal the request
  1087.         for a legal value using an interrupt.The interrupt routine then
  1088.         simply takes the value directly.This method is also employed for
  1089.         the operation of the serial interface(RS232).
  1090.         We'll talk about the use of interrupts at a later time.The last
  1091.         thing we want to mention about interrupts at this time is that,
  1092.         like exceptions,interrupt routines are terminated with the RTE
  1093.         instruction.
  1094.  
  1095.       2.3.4.Condition Codes.
  1096.       ---------------------
  1097.         When you write a program in any language,the need for a
  1098.         conditional operation arises quite often.For instance,in a BASIC
  1099.         program
  1100.  
  1101.             IF D1=2 THEN D2=0
  1102.  
  1103.         represents a conditional operation.To write the equivalent in
  1104.         machine language,you first need to make the comparison:
  1105.  
  1106.             CMP  #2,D1
  1107.  
  1108.         CMP stands for compare and compares two operands,in this case D1
  1109.         and D2.How is this operation evaluated?
  1110.         For this purpose you have condition codes(CC's),which exist in the
  1111.         branch instructions of machine language.Because of this,you must
  1112.         specify when and where the program is to branch.
  1113.         The simplest variation of the branch instruction is an
  1114.         unconditional branch.The corresponding instruction is 'BRA address
  1115.         ',although this won't help you here.After all,you need a
  1116.         conditional branch.
  1117.         To retain the result of the operation,in this case a comparrison
  1118.         (CMP),several bits are reserved in the status word.Lets look at
  1119.         bit 2 first,which is the zero flag.This flag is set when the
  1120.         result of the previous operation was zero.
  1121.         To explain the relationship between CMP and the Z flag,you must
  1122.         first clarify the function of the CMP instruction.Actually this
  1123.         instruction performs the subtraction of the source operand from
  1124.         the destination operand.In the example above,the number 2 is
  1125.         subtracted from the content of the memory cell at D1.Before the
  1126.         result of this subtraction is discarded,the corresponding flags
  1127.         are set.
  1128.         If the contents of D1 in our example above happened to be 2,the
  1129.         result of the subtraction would be 0.Thus,the Z flag would be set,
  1130.         which can then be evaluated through a corresponding branch
  1131.         instruction.Our example would then look like this:
  1132.  
  1133.                    ...
  1134.                   CMP   #2,D1      ;comparison,or subtraction
  1135.                   BNE   UNEQUAL    ;branch,if not equal(Z flag not set)
  1136.                   MOVE  #0,D2      ;otherwise execute D2=0
  1137.          
  1138.            UNEQUAL:
  1139.  
  1140.                    ...             ;program branches to here
  1141.  
  1142.         BNE stands for Branch if Not Equal.This means,that if the Z flag
  1143.         was cleared(=0)by the previous CMP,the program branches to the
  1144.         address specified by BNE(here represented by UNEQUAL).The counter
  1145.         part to the BNE instruction is the BEQ(Branch if EQual)instruction
  1146.         which is executed if Z=1.
  1147.         Here's a list of condition codes,which allow you to form
  1148.         conditional branches using the Bcc(cc=condition code)format:
  1149.  
  1150.            cc       Condition                     Bits
  1151.            ---------------------------------------------------
  1152.            T        true,corresponds to BRA
  1153.            F        false,never branches
  1154.            HI       higher than                   C'* Z'
  1155.            LS       lower or same                 C + Z
  1156.            CC,HS    carry clear,higher or same    C'
  1157.            CS,LO    carry set,lower               C
  1158.            NE       not equal                     Z'
  1159.            EQ       equal                         Z
  1160.            VC       overflow clear                V'
  1161.            VS       overflow set                  V
  1162.            PL       plus,positive
  1163.            MI       minus,negative
  1164.            GE       greater or equal              N*V+N'*V'
  1165.            LT       less than                     N*V'+N'*V
  1166.            GT       greater than                  N*V*Z'+N'*V'*Z'
  1167.            LE       less or equal                 Z + N*V' + N'*V
  1168.  
  1169.            *=logic AND, +=logic OR, '=logic NOT
  1170.  
  1171.         Here are a few examples to demonstrate how these numerous
  1172.         conditions can be utilized:
  1173.  
  1174.            CMP   #2,D1
  1175.            BLS SMALLER_EQUAL
  1176.  
  1177.         This branches if the contents of D1<=2,whether D1 is 0,1 or 2.In
  1178.         this example,the BLE instruction would allow the program to branch
  1179.         even if D1 is negative.You can tell this by the fact that the V
  1180.         bit is used in the evaluation of this expression(see chart above).
  1181.         When the sign is changed during the operation,this V bit is
  1182.         compared with the N bit.Should both bits be cleared(N bit=0 and V
  1183.         bit=0)after the CMP subtraction(D1-2),the result has remained
  1184.         positive:the condition as not been met.
  1185.         The conditions EQ and NE are quite important for other uses,as
  1186.         well.For instance,they can be used to determine if particular bits
  1187.         in a data word are set,by writing the following sequence...
  1188.  
  1189.             ...
  1190.             AND  #%00001111,D1       ;masks bits out
  1191.             BEQ SMALLER              ;branches when none of the four
  1192.         ;                            ;lower bits is set
  1193.             CMP  #%00001111,D1 
  1194.             BEQ ALL                  ;branches when all four bits set
  1195.  
  1196.         The AND instruction causes all bits of D1 to be compared with the 
  1197.         bits of the parameter(in this case #%00001111).If the same bits
  1198.         are set in both bytes,the corresponding bits are also set in the
  1199.         result.If one bit of the pair is cleared,the resulting bit is zero
  1200.         as well.Thus,in the result,the only bits that are set are those
  1201.         bits of the lowest four that were set in D1.
  1202.         The technique is known as masking.In the example above,only the
  1203.         lowest four bits were masked out,which meansthat in the resulting 
  1204.         byte,only the lowest four appear in their original condition.All
  1205.         other bits are cleared with the AND operand.Of course you can use
  1206.         any bit combination with this method.
  1207.         If no bit at all is set in the result,the zeroflag is set,thus
  1208.         fullfilling the BEQ condition and branching the program.Otherwise,
  1209.         the next instruction is processed,in which D1 is compared with
  1210.         %00001111.When both are equal,at leastall of the four lowest bits
  1211.         of the original byte have been set,in which case the following BEQ
  1212.         instruction branches.
  1213.         Aside from CMP,the CC and CS conditions can also be used to
  1214.         determine whether a HI bit was pushed out of the data word during
  1215.         data rotation with the ROL and ROR instructions.
  1216.         Before you move on the instruction vocabulary of the MC68000,we'd
  1217.         like to give you another tip:
  1218.         The AssemPro assembler makes it quite easy to try every command in
  1219.         posible situations.Take the CMP command which we've been talking
  1220.         about,for example.To test this command with various values and to
  1221.         receive the results of the comparisons directly via the flags,try
  1222.         the following.
  1223.         Type the following into the Editor.
  1224.  
  1225.             run:
  1226.             cmp  $10,d1
  1227.             bra run
  1228.             end
  1229.  
  1230.         Assemble it,save the resulting code and enter the debugger.After
  1231.         reloading the code you can then single step through the program
  1232.         observing the results the program has on the flags.Try changing
  1233.         the values in the register D1 and see how higher and lower values
  1234.         affect the flag.
  1235.         By the way,using the start command at this time causes it to run 
  1236.         forever.Well,at least until reset is hit,which isn't exactly
  1237.         desirable,either...
  1238.         This procedure isn't limited to just the CMP instruction.You can
  1239.         use it to try any other instructions you're interested in.
  1240.  
  1241.       2.4.The 68000 Instructions.
  1242.       --------------------------
  1243.         Its about time to explain the MC68000 instructions.You don't have
  1244.         room for an in-depth discussion of each instruction in this book;
  1245.         for that purpose we recommend PROGRAMMING THE 68000 from Sybex by
  1246.         Steve Williams.
  1247.         The following tables show the required parameters and arguments
  1248.         for each instruction.AssemPro have access to built in help tables
  1249.         covering effective addressing modes and many of the Amiga
  1250.         Intuition calls.The following notation is used for arguments:
  1251.  
  1252.            Label     a label or address
  1253.            Reg       register
  1254.            An        address register n
  1255.            Dn        data register n
  1256.            Source    source operand
  1257.            Dest      destination operand
  1258.            <ea>      address or register
  1259.            #n        direct value
  1260.  
  1261.         Here is a short list of the instructions for the MC68000 processor
  1262.         AssemPro owners can simply place the cursor at the beginning of
  1263.         the instruction and press the help key to see the addressing modes
  1264.         allowed:
  1265.  
  1266.         Mnemonic             Meaning
  1267.         ---------------------------------------------------
  1268.        Bcc     Label         conditional branch,depends on condition
  1269.        BRA     Label         unconditional branch(similar to JMP)
  1270.        BSR     Label         branch to subprogram.Return address is
  1271.                              deposited on stack,RTS causes return to that
  1272.                              address.
  1273.        CHK     <ea>,Dx       check data register for limits,activate the
  1274.                              CHK instruction exception.
  1275.        DBcc    Reg,Label     check condition,decrement and branch
  1276.        JMP     Label         jump to address(similar to BRA)
  1277.        JSR     Label         jump to subroutine.Return address is
  1278.                              deposited on stack,RTS causes return to that
  1279.                              address.
  1280.        NOP                   no operation
  1281.        RESET                 reset peripherals(caution!)
  1282.        RTE                   return from exception
  1283.        RTR                   return with loading of flags
  1284.        RTS                   return from subroutine(after BSR and JSR)
  1285.        Scc     <ea>          set a byte to -1 when condition is met
  1286.        STOP                  stop processing(caution!)
  1287.        TRAP #n               jump to an exception
  1288.        TRAPV                 check overflow flag,the TRAPV exception
  1289.  
  1290.         Here are a few important notes...
  1291.         When a program jumps(JSR)or branches(BSR)to subroutine,the return
  1292.         address to which the program is to return is placed on the stack.
  1293.         At the RTS instruction,the address is pulled back off the stack,
  1294.         and the program jumps to that point.
  1295.         Lets experiment a little with this procedure.Please enter the 
  1296.         following short program:
  1297.  
  1298.           run:
  1299.                  pea     subprogram   ;address on the stack
  1300.                  jsr     subprogram   ;subprogram called
  1301.                  move.l  (sp)+,d1     ;get a long word from stack
  1302.           ;      illegal              ;for assemblers without
  1303.                                       ;debuggers
  1304.  
  1305.           subprogram:
  1306.                  move.l  (sp),d0      ;return address in D0
  1307.                  rts                  ;and return
  1308.                  end
  1309.  
  1310.         The first instruction,PEA,places the address of the subprogram on
  1311.         the stack.Next,the JSR instruction jumps to the subprogram.The
  1312.         return address,or the address at which the main program is to
  1313.         continue after the completion of the subprogram,is also deposited
  1314.         on the stack at this point.
  1315.         In the subprogram,the long word pointed to by the stack pointer is
  1316.         now loaded into the data register D0.After that,the RTS
  1317.         instruction pulls the return address from the stack,and the
  1318.         program jumps to that address.
  1319.         Back in the main program,the long word which is on the top of the
  1320.         stack,is pulled from the stack and written in D1.Assemblers that
  1321.         do not have the debugging features of AssemPro may need the
  1322.         ILLEGAL instruction so they can break the program and allow you to
  1323.         view the register contents.
  1324.         Assemble the program and load the resulting code into the debugger
  1325.         Single step thru the program and examine the register contents.
  1326.         Here you can see that D0 contains the address at which the program
  1327.         is to continue after the RTS command.Also,D1 contains the address
  1328.         of the subprogram which you can verify by comparing the debugger
  1329.         listing.
  1330.         The STOP and RESET instructions are so powerful that they can only
  1331.         be used in supervisor mode.Even if you do switch to the supervisor
  1332.         mode,you should NOT use these instructions if there is any data in
  1333.         memory that has not been saved and you wish to retain.
  1334.         The TRAP instruction receives a number between 0 and $F,which
  1335.         determines the particular TRAP vector(addresses $0080-$00BF)and
  1336.         thus the corresponding exception routine.Several operating systems
  1337.         for the 68000 utilize this instruction to call operating system
  1338.         functions.You'll deal more with this instruction later.
  1339.         In the short sample program that compared two numbers,the CMP
  1340.         instruction performed an arithmetic function,namely a subtraction.
  1341.         This subtraction could be performed with an actual result as well
  1342.         using the SUB instruction.The counterpart to this is in addition,
  1343.         for which the ADD instruction is used.In 8 bit processors,like the
  1344.         6502,these arithmetic functions are the only mathematical
  1345.         operations.The MC68000 can also multiply,divide,and perform these
  1346.         operations with a variety of data sizes.
  1347.         Most of the functions require two parameters.For instance the ADD
  1348.         instruction...
  1349.  
  1350.             ADD source,destination
  1351.  
  1352.         where source and destination can be registers or memory addresses.
  1353.         Source can also be a direct value(#n).The result of the opoeration
  1354.         is placed in the destination register or the destination address.
  1355.         This is same for all operation of this type.These instructions can
  1356.         be tried out with the AssemPro assembler.In this case we recommend
  1357.         the use of a register as the destination.
  1358.         Heres an overview of the arithmetic operations with whole numbers:
  1359.  
  1360.         Mnemonic                   Meaning
  1361.         --------------------------------------------------------------
  1362.         ADD       source,dest      binary addition
  1363.         ADDA      source,An        binary addition to a address register
  1364.         ADDI      #n,<ea>          addition with a constant
  1365.         ADDQ      #n,<ea>          fast addition of a constant which can
  1366.                                    be only from 1-8
  1367.         ADDX      source,dest      addition with transfer in X flag
  1368.         CLR       <ea>             clear an operand
  1369.         CMP       source,dest      comparison of two operands
  1370.         CMPA      <ea>,An          comparison with an address register
  1371.         CMPI      #n,<ea>          comparison with a constant
  1372.         CMPM      source,dest      comparison of two memory operands
  1373.         DIVS      source,dest      sign-true division of a 32 bit
  1374.                                    destination by a 16 bit source operand.
  1375.                                    The result of the division is stored in
  1376.                                    the LO word of the destination,the
  1377.                                    remainder in the HI word.
  1378.         DIVU      source,dest      division without regard to sign,similar
  1379.                                    to DIVS
  1380.         EXT       Dn               sign-true expansion to twice original
  1381.                                    size(width)data unit
  1382.         MULS      source,dest      sign-true multiplication of two words
  1383.                                    into one long word
  1384.         MULU      source,dest      multiplication without regard to sign,
  1385.                                    similar to MULS
  1386.         NEG       <ea>             negation of an operand(twos complement)
  1387.         NEGX      <ea>             negation of an operand with transfer
  1388.         SUB       source,dest      binary subtraction
  1389.         SUBA      <ea>,An          binary subtraction from an address
  1390.                                    register
  1391.         SUBI      #n,<ea>          subtraction of a constant
  1392.         SUBQ      #n,<ea>          fast subtraction of a 3 bit constant
  1393.         SUBX      source,dest      subtraction with transfer in X flag
  1394.         TST       <ea>             test an operand and set N and Z flag
  1395.  
  1396.         For the processing of whole numbers,the processor can operate with
  1397.         BCD numbers.These are Binary Coded Decimal numbers,which means
  1398.         that the processor is working with decimals.In this method,each
  1399.         halfbyte contains only numbers from 0 to 9,so that these numbers
  1400.         can be easily processed.For this method,the following instructions
  1401.         are available:
  1402.  
  1403.         Mnemonic                   Meaning
  1404.         -----------------------------------------------------------------
  1405.         ABCD      source,dest      addition of two BCD numbers
  1406.         NBCD      source,dest      negation of a BCD number(nine
  1407.                                    complement)
  1408.         SBCD      source,dest      subtraction of two BCD numbers
  1409.  
  1410.         Again,we recommend that you try this out yourself.Although
  1411.         handling the BCD numbers is relatively easy,it can be rather
  1412.         awkward at first.Be sure that you enter only BCD numbers for
  1413.         source and destination,since the results are not correct
  1414.         otherwise.
  1415.         Next are the logical operations,which you might know from BASIC.
  1416.         With these functions,you can operate on binary numbers bit for
  1417.         bit.
  1418.  
  1419.         Mnemonic                   Meaning
  1420.         -----------------------------------------------------------------
  1421.         AND       source,dest      logic AND
  1422.         ANDI      #n,<ea>          logic AND with a constant
  1423.         EOR       source,dest      exclusive OR
  1424.         EORI      #n,<ea>          exclusive OR with a constant
  1425.         NOT       <ea>             inversion of an operand
  1426.         OR        source,dest      logic OR
  1427.         ORI       #n,<ea>          logic OR wuth a constant
  1428.         TAS       <ea>             check a byte and set bit 7
  1429.  
  1430.         Single bits can also be manipulated by the following set of
  1431.         instructions:
  1432.  
  1433.         Mnemonic                   Meaning
  1434.         ----------------------------------------------------------------
  1435.         BCHG      #n,<ea>          change bit n(0 is changed to 1 and vice
  1436.                                    versa)
  1437.         BCLR      #n,<ea>          clear bit n
  1438.         BSET      #n,<ea>          set bit n
  1439.         BTST      #n,<ea>          test bit n,result is displayed in Z
  1440.                                    flag
  1441.  
  1442.         These instructions are particularly important from the
  1443.         manipulation and evaluation of data from peripherals.After all,in
  1444.         this type of data,single bits are often very significant.You'll
  1445.         come across this more in later chapters.
  1446.         The processor can also shift and rotate an operand within itself
  1447.         ('n'indicates a register,'#'indicates a direct value which
  1448.         specifies the number of shiftings)...
  1449.  
  1450.         Mnemonic                   Meaning
  1451.         ----------------------------------------------------------------
  1452.         AS        n,<ea>           arithmetic shift to the left(*2^n)
  1453.         ASR       n,<ea>           arithmetic shift to the right(/2^n)
  1454.         LSL       n,<ea>           logic shift to the left
  1455.         LSR       n,<ea>           logic shift to the right
  1456.         ROL       n,<ea>           rotation left
  1457.         ROR       n,<ea>           rotation right
  1458.         ROXL      n,<ea>           rotation left with transfer in X flag
  1459.         ROXR      n,<ea>           rotation right with transfer in X flag
  1460.  
  1461.         All these instructions allow you to shift a byte,a word or a long
  1462.         word to the left or right.Its not too surprising that this is the
  1463.         equivalentof multipling(dividing)the number by a power of 2.Here's
  1464.         a little example to demonstrate why
  1465.         Lets take a byte containing the value 16 as an example.In binary,
  1466.         it looks like this:
  1467.  
  1468.             %00010000 =16
  1469.         
  1470.         Now,if you shift the byte to the left by inserting a 0 at the
  1471.         right,you'll get the following result...
  1472.  
  1473.             %00010000 shifted to the left equals
  1474.             %00100000 =32,in effect 16*2
  1475.  
  1476.         Repeated shifting results in repeated doubling of the number.Thus
  1477.         if you shift the number n times,the number is multiplied by 2^n.
  1478.         The same goes for shifting to the right.However,this operation as
  1479.         a slight quirk:here's a sample byte with the value 5:
  1480.  
  1481.             %00000101 =5,shifted once to the right equals
  1482.             %00000010 =2
  1483.  
  1484.         The answer in this case is not 2.5 as you might expect.The result
  1485.         such a division is always a whole number,since any decimal places
  1486.         are discarded.If you use the DIV instruction instead of shifting,
  1487.         you'll retain the digits to the right of the decimal point.However
  1488.         shifting is somewhat faster,and shifting can also receive long
  1489.         words as results.
  1490.         After explaining the principle of shifting,you still need to know
  1491.         why more than two instructions are required for the procedure.Well
  1492.         this is because there are several different types of shifting.
  1493.         First,you must differentiate between shifting and rotating.In
  1494.         shifting,the bit that is added to the left or the right side is
  1495.         always a zero.In rotating,it is always a specific value that is
  1496.         inserted.This means that with the ROR or the ROL instructions,the
  1497.         bit that is taken out on one side is the one that is inserted on
  1498.         the other.With the ROXR and the ROXL instructions this bit takes a
  1499.         slight detour to the X flag.Thus,the content of the flag is
  1500.         inserted into the new bit,while the old bit is loaded into the 
  1501.         flag.
  1502.         Shifting as well,has two variations:arithmetic and logical
  1503.         shifting.You've already dealt with logical shifting.In this
  1504.         variation,the inserted bit is always a zero,and the extracted bit
  1505.         is deposited in the C flag and in the X flag.
  1506.         Although the highest bit,which always represents the sign,is
  1507.         shifted in arithmetic shifting,the sign is still retained by ASR.
  1508.         This has the advantage that when these instructions are used for
  1509.         division,the operation retains the correct sign(-10/2 equals-5).
  1510.         However,should an overflow or underflow cause the sign to change,
  1511.         this change is noted in the V flag,which always indicates a change
  1512.         in sign.With logical shifting this flag is always cleared.
  1513.         Now to the instructions that allow you to move data.These are
  1514.         actually the most important instructions for any processor,for how
  1515.         else could you process data?
  1516.  
  1517.         Mnemonic                   Meaning
  1518.         ------------------------------------------------------------------
  1519.         EXG       Rn,Rn            exchange of two register contents(don't
  1520.                                    confuse with swap)
  1521.         LEA       <ea>,An          load an effective address in address
  1522.                                    register An
  1523.         LINK      An,#n            build stack range
  1524.         MOVE      source,dest      carry value over from source to dest
  1525.         MOVE      SR,<ea>          transfer the status register contents
  1526.         MOVE      <ea>,SR          transfer the status register contents
  1527.         MOVE      <ea>,CCR         load flags
  1528.         MOVE      USP,<ea>         transfer the user stack point
  1529.         MOVE      <ea>,USP         transfer the user stack point
  1530.         MOVEA     <ea>,An          transfer a value to the address
  1531.                                    register An
  1532.         MOVEM     Regs,<ea>        transfer several registers at once
  1533.         MOVEM     <ea>,Regs        transfer several registers at once
  1534.         MOVEP     source,dest      transfer data to peripherals
  1535.         MOVEQ     #n,Dn            quickly transfer a 8 bit constant to
  1536.                                    the data register Dn
  1537.         PEA       <ea>             deposit an address on the stack
  1538.         SWAP      Dn               swap the halves of the register(the
  1539.                                    upper 16 bits with the lower)
  1540.         UNLK      An               unlink the stack
  1541.  
  1542.         The LEA or PEA instructions are often used to deposit addresses in
  1543.         an address register or on the stack.The instruction
  1544.  
  1545.              LEA   label,A0
  1546.  
  1547.         loads the address of the label'label' into the address register
  1548.         A0.In practice,this corresponds to
  1549.  
  1550.              MOVE.L  #label,A0
  1551.  
  1552.         which is equivalent to
  1553.  
  1554.              PEA  label
  1555.  
  1556.         All these instructions deposit the address of 'label' on the stack
  1557.         The following instruction also does this:
  1558.  
  1559.              MOVE.L  #label,-(SP)
  1560.  
  1561.         The LEA instruction becomes much more interesting when the label
  1562.         is replaced by indirect addressing.Here's an example:
  1563.  
  1564.              LEA   1(A0,D0),A1
  1565.  
  1566.         The address that's produced by the addition of 1(direct value
  1567.         offset)+A0+D0 is located in A1.To duplicate this instruction with
  1568.         MOVE would be quite cumbersome.Take a look:
  1569.  
  1570.              MOVE.L  A0,A1
  1571.              ADD.L   D0,A1
  1572.              ADDQ.L  #1,A1
  1573.  
  1574.         As you can see,the LEA instruction offers you quite some
  1575.         interesting possibilities.
  1576.         Those are all the instructions of the MC68000.Through their
  1577.         combination using the diverse methods of addressing,you can create
  1578.         a great number of different instructions,in order to make a
  1579.         program as efficent as possible.
  1580.         The following table is an overview of all MC68000 instructions
  1581.         along with their possible addressing types and the influence of
  1582.         flags.The following abbreviations are used:
  1583.  
  1584.             x=legal          s=source only    d=destination only
  1585.             -=not effected   0=cleared        *=modified accordingly
  1586.             1=set            u=undermined     P=privileged
  1587.  
  1588.         Mnemonic     1  2  3  4  5  6  7  8  9 10 11 12  X N Z V C   P
  1589.         -----------------------------------------------------------------
  1590.         ABCD         x
  1591.         ADD          s  s  x  x  x  x  x  x  x  s  s  s  * * * * *
  1592.         ADDA         x  x  x  x  x  x  x  x  x  x  x  x  - - - - -
  1593.         ADDI         x     x  x  x  x  x  x  x           * * * * *
  1594.         ADDQ         x  x  x  x  x  x  x  x  x           * * * * *
  1595.         ADDX         x           x                       * * * * *
  1596.         AND          s     x  x  x  x  x  x  x  s  s  s  - * * 0 0
  1597.         ANDI         x     x  x  x  x  x  x  x           - * * 0 0
  1598.         ASL, ASR     x     x  x  x  x  x  x  x           * * * * *
  1599.         Bcc                                              - - - - -
  1600.         BCHG         x     x  x  x  x  x  x  x           - - * - -
  1601.         BCLR         x     x  x  x  x  x  x  x           - - * - -
  1602.         BRA                                              - - - - -
  1603.         BSET         x     x  x  x  x  x  x  x           - - * - - 
  1604.         BSR                                              - - - - -
  1605.         BTST         x     x  x  x  x  x  x  x  z  x  x  - - * - - 
  1606.         CHK          x     x  x  x  x  x  x  x  x  x  x  - * u u u
  1607.         CLR          x     x  x  x  x  x  x  x           - 0 1 0 0
  1608.         CMP          x  x  x  x  x  x  x  x  x  x  x  x  - * * * *
  1609.         CMPA         x  x  x  x  x  x  x  x  x  x  x  x  - * * * * 
  1610.         CMPI         x     x  x  x  x  x  x  x           - * * * *
  1611.         CMPM               x        x  x  x  x     x  x  - * * *
  1612.         cpGEN                                            - - - - -
  1613.         DBcc                                             - - - - -
  1614.         DIVS         x     x  x  x  x  x  x  x  x  x  x  - * * * 0
  1615.         DIVU         x     x  x  x  x  x  x  x  x  x  x  - * * * 0
  1616.         EOR          x     x  x  x  x  x  x  x           - * * 0 0
  1617.         EORI         x     x  x  x  x  x  x  x           - * * 0 0
  1618.         EORI CCR                                         * * * * *
  1619.         EORI SR                                          * * * * *
  1620.         EXG                                              - - - - -
  1621.         EXT                                              - * * 0 0
  1622.         EXTB                                             - * * 0 0
  1623.         ILLEGAL                                          - - - - -
  1624.         JMP                x        x  x  x  x     x  x  - - - - -
  1625.         JSR                x        x  x  x  x     x  x  - - - - -
  1626.         LEA                x        x  x  x  x     x  x  - - - - -
  1627.         LINK            x                                - - - - -
  1628.         LSL, LSR           x  x  x  x  x  x  x           * * * 0 *
  1629.         MOVE         x  s  x  x  x  x  x  x  x  s  s  s  - * * 0 0
  1630.         MOVEA        x  x  x  x  x  x  x  x  x  x  x  x  - - - - -
  1631.         MOVE to CCR  x     x  x  x  x  x  x  x  x  x  x  * * * * *
  1632.         MOVE from SR x     x  x  x  x  x  x  x           - - - - -   P
  1633.         MOVE to SR   x     x  x  x  x  x  x  x  x  x  x  * * * * *   P
  1634.         MOVE USP        x                                - - - - -   P
  1635.         MOVEM              x  s  d  x  x  x  x     s  s  - - - - -
  1636.         MOVEP        s  d                                - - - - -
  1637.         MOVEQ        d                                   - * * 0 0
  1638.         MULS         x     x  x  x  x  x  x  x  x  x  x  - * * 0 0
  1639.         MULU         x     x  x  x  x  x  x  x  x  x  x  - * * 0 0
  1640.         NBCD         x     x  x  x  x  x  x  x           * u * u *
  1641.         NEG          x     x  x  x  x  x  x  x           * * * * *
  1642.         NEGX         x     x  x  x  x  x  x  x           * * * * * 
  1643.         NOP                                              - - - - -
  1644.         NOT          x     x  x  x  x  x  x  x           - * * 0 0
  1645.         OR           s     x  x  x  x  x  x  x  s  s  s  - * * 0 0
  1646.         ORI          x     x  x  x  x  x  x  x           - * * 0 0
  1647.         PEA                x        x  x  x  x     x  x  - - - - -
  1648.         RESET                                            - - - - -   P
  1649.         ROL, ROR           x  x  x  x  x  x  x           - * * 0 *
  1650.         ROXL, ROXR         x  x  x  x  x  x  x           - * * 0 *
  1651.         RTE                                              - - - - -   P
  1652.         RTR                                              * * * * *
  1653.         RTS                                              - - - - - 
  1654.         SBCD         x           x                       * u * u *
  1655.         Scc          x     x  x  x  x  x  x  x           - - - - -
  1656.         STOP                                    x        - - - - -
  1657.         SUB          s  s  x  x  x  x  x  x  x  s  s  s  * * * * *
  1658.         SUBA         x  x  x  x  x  x  x  x  x  x  x  x  - - - - -
  1659.         SUBI         x     x  x  x  x  x  x  x           * * * * *
  1660.         SUBQ         x  x  x  x  x  x  x  x  x           * * * * *
  1661.         SUBX         x           x                       * * * * *
  1662.         SWAP         x                                   - * * 0 0
  1663.         TAS          x     x  x  x  x  x  x  x           - * * 0 0
  1664.         TRAP                                    x        - - - - -
  1665.         TRAPV                                            - - - - -
  1666.         TST          x     x  x  x  x  x  x  x           - * * 0 0
  1667.         UNLK            x                                - - - - -          
  1668.         
  1669.  
  1670.  
  1671.  
  1672.       Chapter 3.
  1673.       ---------
  1674.       3.Working With Assemblers.
  1675.       -------------------------
  1676.         The instructions that you've learned so far are incomprehensible
  1677.         to the MC68000 processor.The letters MOVE mean absolutely nothing
  1678.         to the processor-it needs the instructions in binary form.Every
  1679.         instruction must be coded in a word-which normally takes a lot of
  1680.         work.
  1681.         An assembler does this work for you.An assembler is a program that
  1682.         translates the instructions from text into the coresponding binary
  1683.         instructions.The text that is translated is called Mnemonic or
  1684.         Memcode.Its a lot easier working with text instructions-or does
  1685.         $4280 mean more to you than CLR.L D0?
  1686.         This chapter is about working with assemblers.We'll describe the
  1687.         following three:
  1688.       ASSEM;
  1689.         This is the assembler from the Amiga's development package.This
  1690.         assembler is quite powerful,but it is clearly inferior to its two
  1691.         fellow compilers in some areas.
  1692.       AssemPro;
  1693.         This is the Abacus assembler.It has a debugger in addition to the
  1694.         assembler.This lets you test and correct programs.In our opinion,
  1695.         it is the best one to use for writing and testing practice
  1696.         programs.For this reason,we wrote the programs in this book with
  1697.         this assembler.
  1698.       KUMA-SEKA;
  1699.         This is a popular assembler which also has a debugger.
  1700.  
  1701.         All assemblers perform a similar task-they translate memcode,so
  1702.         that you can write a runable program to disk.To test the program
  1703.         directly,you need a debugger which is something Assem doesn't
  1704.         have.
  1705.  
  1706.       3.1.The Development Assembler.
  1707.       -----------------------------
  1708.         This assembler is a plain disk assembler.That means that it can
  1709.         only assemble text files that are on disk and write the result
  1710.         back to disk.You can't make direct input or test run the new
  1711.         program.
  1712.         You can call ASSEM from the CLI by typing ASSEM followed by
  1713.         parameters that specify what you wish the assembler to do.
  1714.         In the simplest case,you call it like this:
  1715.  
  1716.              ASSEM Source -O Destination
  1717.  
  1718.         Source is the filename of the file containing the program text.
  1719.         Destination is the name of the file that contains the results of
  1720.         assembling after the process is over.The "-O"means that the
  1721.         following name is used for the object file.
  1722.         There are several other parameters that can be passed.These are
  1723.         written with their option(ie-O),so that the assembler knows what
  1724.         to do with the file that you've told it to use.The following
  1725.         possible options must be followed by a filename.
  1726.  
  1727.             -O    Object file
  1728.             -V    Error messages that occur during assembling are written
  1729.                   to a file.If this isn't given,the error messages appear
  1730.                   in the CLI window.
  1731.             -L    The output of the assembled program lines are sent to
  1732.                   this file.You can also use"PRT:"to have it printed.
  1733.             -H    This file is read in at the beginning of the assembled
  1734.                   file and assembled along with it.
  1735.             -E    A file is created that contains lines which have EQU
  1736.                   instructions.
  1737.             -C    This option isn't followed by a filename but by another
  1738.                   option.You can also use OPT to do this.The following
  1739.                   options are available:
  1740.                   
  1741.                   OPT S  A symbol table is created which contains all the
  1742.                          labels and their values.
  1743.                   OPT X  A cross reference list is created(where labels
  1744.                          are used).
  1745.                   OPT W  A number must follow this option.It sets the
  1746.                          ammount of workspace to be reserved.
  1747.  
  1748.         The assembler creates an object file.This is not runnable.To make
  1749.         it runnable,you need to call the linker,ALINK.This program can
  1750.         link several assembled or compiled object files together to make a
  1751.         runnable program.In the simplest case,you enter the following
  1752.         instruction in the CLI:
  1753.  
  1754.              ALINK Source TO Destination
  1755.  
  1756.         Source is the object file produced by the assembler.Destination is
  1757.         the name of the program.It can be started directly.
  1758.  
  1759.       3.2.AssemPro.
  1760.       ------------
  1761.         Abacus's AssemPro is a package which combines editor,assembler and
  1762.         debugger in an easy to use package.
  1763.         The AssemPro Program is divided into several windows-one for the
  1764.         assembler,the editor,the debugger and several help functions.
  1765.         Producing a program is very easy:
  1766.           
  1767.           1)  Write the program with the editor and then store it to disk.
  1768.           2)  Start the assembler,so that the program is translated.
  1769.           3)  If desired,start the debugger,load the program and test it.
  1770.  
  1771.         Within the debugger you can work through parts of the program or
  1772.         even through single commands.As after each one of these steps the
  1773.         debugger shows you the state of the registers and flags in the
  1774.         status register,you can easily try the programs presented in this
  1775.         book.
  1776.         You need to load AssemPro only once when working with machine
  1777.         language programs.Thus you don't need to save back and to between
  1778.         editor,assembler,linker and debugger.
  1779.         AssemPro has an especially interesting function:the reassembler.
  1780.         With this debugger function you are able to convert runnable
  1781.         programs into the source text of the program.Once you have made
  1782.         the source text,you can edit the program using the editor and
  1783.         assemble it again.AssemPro is equipped with functions other
  1784.         assemblers miss.There are however,some differences you should know
  1785.         about.As many programs you see were written for the K-SEKA,be
  1786.         aware of one difference:the EVEN command.AssemPro uses the ALIGN
  1787.         instruction.
  1788.         Note,that when entering and assembling one of the programs in
  1789.         AssemPro you must make sure that you place an END directive at the
  1790.         end of the source text.
  1791.         The following is an introduction into working with AssemPro and
  1792.         the programs in this book.
  1793.         
  1794.         Start AssemPro normally,next click on the editor window and start
  1795.         typing in your program.If the program is on disk already,load it
  1796.         by selecting the appropriate menu or by using the key combination
  1797.         right <AMIGA> key and <o>.To do this you only need to ckick on the
  1798.         filename in the displayed requester and click on the OK gadget.
  1799.         Once you have typed in or loaded the program into the editor,you
  1800.         can assemble it.It is best to save your source before assembling.
  1801.         You assemble your program by clicking on the assembler window
  1802.         displayed above the editor window and pressing <AMIGA> and <a>.You
  1803.         can then choose how to locate your program in the memory.Remember
  1804.         that data used by the co-processors must be located in CHIP RAM.
  1805.         By clicking OK you start the assembler process.If you additionally
  1806.         select "breakable",you can cancel the process by pressing both
  1807.         shift keys.If any error occurs during assembling,AssemPro uses a
  1808.         window to tell you this.Use this window to correct the error and
  1809.         continue with "Save and try again".
  1810.         Now the runnable program is located in the Amiga's memory.Use the
  1811.         menu item "Save as"to save it on disk.If you want to store it on
  1812.         RAM disk,click the given filename and enter RAM: in front of this
  1813.         name.In addition you can click on the menu item "ICON"and choose
  1814.         if you only want the program itself on disk but the icon too.Use
  1815.         this icon to start the program at a later time from Workbench.
  1816.         To test-run the program,you move the debugger window to the 
  1817.         foreground of the screen(for instance by clicking on the back
  1818.         gadget).Use "Load"in the debugger menu or <AMIGA> <o> to call the
  1819.         select file window,where you select the saved program.The program
  1820.         is then loaded into the memory and its shown disassembled.
  1821.         The highlighted line(orange)represents the current state of the 
  1822.         program counter.This is the line where the processor reads its
  1823.         next instruction,provided you tell the processor so.There are
  1824.         three ways to do so.
  1825.         The first one is to start the program with "Start".This
  1826.         alternative does not enable you to stop the program if anything
  1827.         goes wrong.
  1828.         The second possibility,"Start breakable"is better in this respect.
  1829.         After the program starts,it continuously displays the registers
  1830.         contents on the left side of the window.In addition to that you
  1831.         can cancel the process by pressing <ESC>.Note that this only works
  1832.         if your program doesn't use the <ESC>key itself.
  1833.         The third possibility enables you to only partly run your program.
  1834.         You can do this by stepping through the program or by placing
  1835.         breakpoints throughout the program.You place these by clicking on 
  1836.         the desired address and then pressing <AMIGA> <b>."BREAKPOINT"is
  1837.         displayed where the command was displayed before.If you start the
  1838.         program now,it stops whenever it comes across any breakpoints.
  1839.         You can start a small part of the program by moving the mouse
  1840.         pointer to the orange line,clicking the left button and holding it
  1841.         down while you drag the mouse pointer downward.If you release the
  1842.         button,the processor works through this part of the program,
  1843.         stopping at the line,where you positioned the mouse pointer.This
  1844.         is a very useful method to step by step test a program.
  1845.         AssemPro as another helpful window:the Table.This window lists the
  1846.         valid address methods for instructions and the parameters of Amiga
  1847.         functions.This is extremely helpful whenever you are not sure
  1848.         about one of the instructions.
  1849.  
  1850.       3.3.The K-SEKA Assembler.
  1851.       ------------------------
  1852.         The SEKA assembler,from KUMA,has a simple text editor and a
  1853.         debugger in addition to the assembler.This program is controlled
  1854.         by simple instructions and it is easy to use.It is also multi-
  1855.         functional and quick,so it is great for small test and example
  1856.         programs.You can use it to write bigger programs once you've got
  1857.         use to the editor.Now lets look at the editor.
  1858.         To load a program as source code(text)into the editor,enter"r"
  1859.         (read).The program asks you for the name of the file with the
  1860.         "<FILENAME>"prompt.You then enter the name of the text file.If you
  1861.         created the file with SEKA,the file is stored on disk with".s" on
  1862.         the end of its name.You don't need to include the".s"when you load
  1863.         the file.Thats taken care of automatically.("s"stands for source.)
  1864.         You can store programs you've just written or modified by using
  1865.         the"w"instruction.The program asks you for the name.If you enter
  1866.         "Test",the file is written to the disk with"Test.s"as its name.
  1867.         This is a normal text file in ASCII format.
  1868.         There are two ways to enter or change a programs:using the line
  1869.         editor or the screen editor.You can enter the second by hitting
  1870.         the <ESC>key.The upper screen section is then reserved for the
  1871.         editor.You can move with the cursor keys and change the text
  1872.         easily.The lines that you enter are inserted into the existing
  1873.         text and automatically numbered.By hitting the <ESC>key again,you
  1874.         leave the screen editor. 
  1875.         There's really not much to say about this editor.It's really just
  1876.         for simple insertions and changes.Other functions are called in
  1877.         normal instruction mode,the mode in which">"is the input prompt.
  1878.         The following instructions are available to you for text editing
  1879.         (<n>stands for a number.The meaning of the instructions is in
  1880.         parenthesis.)
  1881.  
  1882.         Instruction        Function
  1883.         ----------------------------------------------------------------
  1884.         t(Target)          Puts the cursor on the highest line in the
  1885.                            text.
  1886.         t<n>               Puts the cursor on line n.
  1887.         b(Bottom)          Puts the cursor on the last line in the text.
  1888.         u(Up)              Go up one line.
  1889.         u<n>               Go up n lines.
  1890.         d(Down)            Go down one line.
  1891.         d<n>               Go down n lines.
  1892.         z(Zap)             Deletes the current line.
  1893.         z<n>               Deletes n lines starting at the cursor line.
  1894.         e(Edit)            Lets you edit the current line(and only that
  1895.                            line).
  1896.         e<n>               Edit from line n.
  1897.         ftext(Find)        Searches for the text entered starting at the
  1898.                            current line.The case of a letter makes a
  1899.                            difference,so make sure to enter it correctly.
  1900.                            Blanks that appear after the f are looked for
  1901.                            as well!
  1902.         f                  Continues searching beyond the text that was
  1903.                            previously given.
  1904.         i(Insert)          Starts the line editor.Now you can enter a
  1905.                            program line by line.However,you can't use the
  1906.                            cursor to move into another line.Line numbers
  1907.                            are generated automatically.The lines that
  1908.                            follow are moved down,not erased.
  1909.         ks(Kill Source)    The source text is deleted if you answer"y"
  1910.                            when the program asks if you are sure.Otherwise
  1911.                            nothing happens.
  1912.         o(Old)             Cancels the "ks"function and saves the old text
  1913.         p(Print)           Prints the current line.
  1914.         p<n>               Prints n lines starting at cursor line.
  1915.  
  1916.         Those are the K-SEKA's editor functions.In combination with the
  1917.         screen editor,they allow for simple text editing.You can,for
  1918.         example,delete the current line(and other lines)while working in
  1919.         the screen editor by hitting <ESC> to get into instruction mode
  1920.         and then entering"z"(or "z<n>").
  1921.         If you'd like to edit all lines that contain "trap",for example,
  1922.         you can do the following:
  1923.  
  1924.            -Jump to the beginning of the text using "t"
  1925.            -Search for a "trap"instruction by entering "ftrap" in the
  1926.             first line.
  1927.            -Press <ESC> and edit the line.
  1928.            -Press <ESC> again to get into instruction mode.
  1929.            -Search using "f",<ESC>,etc.until you get to the end of the
  1930.             text.
  1931.  
  1932.         This sounds very clumsy,but in practise it works quite well and
  1933.         goes quickly.Experiment with the editor bit,so you can get use to
  1934.         it.
  1935.         Now here are the instructions for working with disks:
  1936.  
  1937.         Instruction             Function
  1938.         -----------------------------------------------------------------
  1939.         v(View files)           Look at the disk's directory.You can also
  1940.                                 include the disk drive or subdirectory
  1941.                                 that interests you.For example,"vc"causes
  1942.                                 the "c"subdirectory to be listed and makes
  1943.                                 it the current directory.
  1944.         kf(Kill file)           The program asks for the name of the file.
  1945.                                 The file is deleted(and you aren't asked
  1946.                                 if your sure either-so be careful).
  1947.         r(Read)                 After inputting this instruction,you'll be
  1948.                                 asked which file to load(FILENAME>).The
  1949.                                 file that you specify is then loaded.If
  1950.                                 only "r"is entered,a text file is loaded
  1951.                                 in the editor.
  1952.         ri(Read Image)          Loads a file into memory.After you've
  1953.                                 entered the filename,SEKA asks for the
  1954.                                 address the file should begin at in memory
  1955.                                 (BEGIN>)and the highest address that
  1956.                                 should be used for the file(END>).
  1957.         rx(Read from Auxillary) This works just like the "ri"function
  1958.                                 except that it reads from the serial port
  1959.                                 instead of from disk(You don't need a file
  1960.                                 name).
  1961.         rl(Read Link file)      This instruction reads in a SEKA created 
  1962.                                 link file.First you'll be asked if you are
  1963.                                 sure,because the text buffer is erased
  1964.                                 when the link file is loaded.
  1965.         w(Write)                After entering this instruction,you'll be 
  1966.                                 asked for the name of the file the text
  1967.                                 should be written to.A".s"is automatically
  1968.                                 appended to the name,so that it can be 
  1969.                                 recognized as a SEKA file.
  1970.         wi(Write Image)         Stores a block of memory to disk after the
  1971.                                 name,beginning and end are entered.
  1972.         wx(Write to Auxillary)  This is similar to"wi";the only difference
  1973.                                 is that the output is to the serial inter-
  1974.                                 face.
  1975.         wl(Write Link file)     Asks for the name and then stores a link 
  1976.                                 file that was assembled with the"I"option
  1977.                                 to disk.If this isn't available,the
  1978.                                 message "* * Link option not specified"
  1979.                                 appears.
  1980.  
  1981.         Once you've typed in or loaded a program,you can call the
  1982.         assembler and have the program translated.Just enter"a"to do so.
  1983.         You'll then be asked which options you want to use.If you enter a
  1984.         <RETURN>,the program is assembled normally-ie the results of
  1985.         translating a program in memory is stored in memory.Then the
  1986.         program can be executed straight away.
  1987.         You can enter one or more of the following options,however:
  1988.  
  1989.           v     The output of the results goes to the screen.
  1990.           p     or
  1991.           e     goes to the printer with a title line.
  1992.           h     The output stops after every page and waits for a key
  1993.                 stroke.This is useful for controlling output to the screen
  1994.                 or for putting new sheets of paper in the printer.
  1995.           o     This option allows the assembler to optimize all possible
  1996.                 branch instructions.This allows the program code to be
  1997.                 shorter than it would otherwise be.Several messages appear
  1998.                 but you can ignore them.
  1999.           l     This option causes linkable code to be produced.You can
  2000.                 save it with the"wl"instruction and read it with the "rl"
  2001.                 instruction.
  2002.  
  2003.         A symbol table is included at the end of the listing if desired.
  2004.         The table contains all labels and their values.It also contains
  2005.         macro names.A macro allows several instructions to be combined in
  2006.         to a single instruction.
  2007.         For example,suppose you wrote a routinethat outputs the text that 
  2008.         register A0 points to.Every time you need to use the routine,you
  2009.         must type:
  2010.  
  2011.             lea  text,a0      ;pointer to text in A0
  2012.             bsr  pline        ;output text
  2013.  
  2014.         You can simplify this by defining a macro for this function.To do
  2015.         this,put the following at the beginning of the program:
  2016.  
  2017.             print:macro     ;Macro with the name "Print"
  2018.              lea  ?1,a0     ;Parameter in A0
  2019.              bsr  pmsg      ;Output text
  2020.             endm            ;End of macro
  2021.  
  2022.         Now,you can simply write the following in your program:
  2023.  
  2024.             print text       ;Output text
  2025.  
  2026.         This line is replaced using the macro during assembly.The
  2027.         parameter "text"is inserted where "?1"appears in the macro.You can
  2028.         have several parameters in a macro.You give them names like "?2",
  2029.         "?3",etc...
  2030.         You can also decide whether you'd like to see the macros in the
  2031.         output listing of the assembler.This is one of the pseudo-ops that
  2032.         are available in the assembler.The SEKA assembler has the
  2033.         following pseudo-ops:
  2034.  
  2035.           dc      Defines one or more data items that should appear in
  2036.                   this location in the program.The word length can be
  2037.                   specified with .B,.W,or .L-and if this is left off, .B
  2038.                   is used.Text can be entered in question marks or
  2039.                   apostrophes.For example:dc.b "Hello",10,13,0
  2040.           blk     Reserves a number of bytes,words or long words,depending
  2041.                   on whether .B,.W,or .L is chosen.The first parameter
  2042.                   specifies the number of words to be reserved.The second
  2043.                   (which is optional)is used to fill the memory area.For
  2044.                   example:blk.w 10,0
  2045.           org     The parameter that follows the org instruction is the
  2046.                   address from which the (absolute) program should be
  2047.                   assembled.For example: org $40000
  2048.           code    Causes the program to be assembled in relative mode,the
  2049.                   mode in which a program is assembled starting at address
  2050.                   0.The Amiga takes care of the new addressing after the
  2051.                   program is loaded.
  2052.           data    This means that from here on only data appear.This can
  2053.                   be left out.
  2054.           even    Makes the current address even by sometimes inserting a
  2055.                   fill byte.
  2056.           odd     The opposite of even-it makes the address odd.
  2057.           end     Assembling ends here.
  2058.           equ or  Used for establishing the value of a label
  2059.           =       For example: Value=123 or Value:equ 123
  2060.           list    Turns the output on again(after nlist).You can use the
  2061.                   following parameters to influence the output:
  2062.                     c    Macro calls
  2063.                     d    Macro definitions
  2064.                     e    Macro expansion of the program
  2065.                     x    Code expansions
  2066.                   For example: list e
  2067.           nlist   Turns off output.You can use the same parameters here as
  2068.                   with "list".
  2069.           page    Causes the printer to execute a page feed,so that you'll
  2070.                   start a new page.
  2071.           if      The following parameter decides whether you should
  2072.                   continue assembling.If it is zero,you won't continue
  2073.                   assembling.
  2074.           else    If the "if"parameter is zero,you'll begin assembling
  2075.                   here.
  2076.           endif   End of conditional assembling.
  2077.           macro   Start of a macro definition.
  2078.           endm    End of macro definition.
  2079.           ?n      The text in the macro that is replaced by the nth
  2080.                   parameter in the calling line.
  2081.           ?0      Generates a new three digit number for each macro call-
  2082.                   this is very useful for local labels.
  2083.                   For example: x?0:bsr pmsg
  2084.           illegal Produces an illegal machine language instruction.
  2085.           globl   Defines the following label as globel when the "I"option
  2086.                   of the assembler is chosen.
  2087.  
  2088.         Once you've assembled your program,the program code is in memory.
  2089.         Using the "h"instruction,you can find out how large the program is
  2090.         and where it is located in memory.The beginning and end address is
  2091.         given in hex and the length in decimal(according to the last
  2092.         executed operations):
  2093.  
  2094.            Work   The memory area defined in the beginning
  2095.            Src    Text in memory
  2096.            RelC   Relocation table of the program
  2097.            RelD   Relocation table of the memory area
  2098.            Code   Program code produced
  2099.            Data   The program's memory area
  2100.  
  2101.         You'll find program in memory at the location given by Code.It's a
  2102.         pain to have to enter this address whenever you want to start the
  2103.         program.It make's good sense to mark the beginning of the program
  2104.         with a label(for example,"run:").You can use the "g"instruction to
  2105.         run the program as follows:
  2106.  
  2107.            g run
  2108.  
  2109.         The"g"(GO)instruction is one of SEKA's debugger instrucions.Heres
  2110.         an overview:
  2111.  
  2112.          x    Output all registers.
  2113.          xr   Output and change of registers(ie xd0)
  2114.          gn   Jump to address n.You`ll be asked for break points,addresses
  2115.               at which the program should be terminate.
  2116.          jn   This is similar to the one above-a JSR is used to jump into
  2117.               the program.The program must end with a RTS instruction.
  2118.          qn   Output the memory starting at address n.You can also specify
  2119.               the word length.For example: q.w $10000
  2120.          nn   Disassembled output starting at address n.
  2121.          an   Direct assembling starting at address n.Direct program
  2122.               instructions are entered.
  2123.          nn   Modify the contents of memory starting at address n.Here too
  2124.               the word length can be given.You can terminate input with
  2125.               the <ESC> key.
  2126.          sn   Executes the program instruction that the PC points to.After
  2127.               you enter this instruction,n program steps are executed.
  2128.          f    Fill a memory area.You can choose the word width.All the
  2129.               needed parameters are asked for individually.
  2130.          c    Copies one memory area to another.All the needed parameters
  2131.               are asked for individually.
  2132.          ?    Outputs the value of an expression or a label.
  2133.               For example: ?run+$1000-256
  2134.          a    Sets an instruction sequence that is passed to the program
  2135.               when it starts as if it were started from CLI with this
  2136.               sequence.
  2137.          !    Leaves the SEKA assembler after being asked if your sure.
  2138.  
  2139.         You saw some of the calculations like SEKA can make in the "?"
  2140.         example.You can also use them in programming.The folowing
  2141.         operations work in SEKA:
  2142.  
  2143.            +  Addition
  2144.            -  Subtraction
  2145.            *  Multiplication
  2146.            /  Division
  2147.            &  Logic AND
  2148.            !  Logic OR
  2149.            ~  EXclusive OR (XOR) 
  2150.  
  2151.         These operations can also be combined.You can choose the counting
  2152.         system.A "$"stands for hexadecimal,"@"for octal,and "%"for binary.
  2153.         If these symbols aren`t used,the number is interpreted as a
  2154.         decimal number.
  2155.         Lets go back to the debugger.As mentioned,after entering "g
  2156.         address",you`ll be asked for break points.You can enter up to 16
  2157.         addresses at which the program halts.If you don`t enter break
  2158.         points,but instead hit <RETURN>,the program must end with an
  2159.         ILLEGAL instruction.If it ends instead with a RTS,the next return
  2160.         address from the stack is retrieved and jumped to.This is usually
  2161.         address 4 which causes SEKA to come back with "**Illegal
  2162.         Instruction at $000004",but theres no guarantee that it will.Your
  2163.         computor can end up so confused that it can`t function.
  2164.         The SEKA program puts an ILLEGAL instruction in the place
  2165.         specified as break points after saving the contents of these
  2166.         locations.If the processor hits an illegal instruction,it jumps
  2167.         back to the debugger by using the illegal instruction vector that
  2168.         SEKA set up earlier.Then SEKA repairs the modified memory
  2169.         locations and then displays the status line.Here you can find out
  2170.         where the program terminated.
  2171.         Using break points is a good technique for finding errors in the
  2172.         program.You can,for example,put a break point in front of a
  2173.         routine that you`re not sure about and start the program.When the
  2174.         program aborts at this spot,you can go through the routine step by
  2175.         step using the "s"option.Then you can watch what happens to the
  2176.         status line after each instruction and find the mistake.
  2177.         Program errors are called bugs.That`s why the program that finds
  2178.         them is called a debugger.  
  2179.  
  2180.  
  2181.  
  2182.  
  2183.       Chapter 4.
  2184.       ---------
  2185.       4.Our First Programs.
  2186.       --------------------
  2187.         You`re getting pretty far along in your knowledge of machine
  2188.         language programming.In fact,you`re to the point where you can
  2189.         write programs,and not just programs for demonstration purposes,
  2190.         but ones that serve a real function.We`re assuming that you have
  2191.         the AssemPro assembler and have loaded it.
  2192.         If you`re using a different assembler,a few things must be done
  2193.         differently.We covered those differences already in the chapter on
  2194.         the different assemblers.
  2195.         We`ve written the example programs as subroutines so they can be
  2196.         tried out directly and used later.After assembling the program,you
  2197.         can put the desired values in the register.Then you can either
  2198.         single-step thru the programs or run the larger programs and
  2199.         observe the results in the registers directly.(using the SEKA
  2200.         assembler you can type "j program_name"to start the program.Then
  2201.         you can read the results from the register directly,or use "q
  2202.         address"to read it from memory.)
  2203.         Lets start with an easy example,adding numbers in a table.
  2204.  
  2205.       4.1.Adding Tables.
  2206.       -----------------
  2207.         Imagine that you have numbers in memory that you'd like to add.
  2208.         Lets assume that you have five numbers whose length is one word
  2209.         each.You want their sum to be written in register D0.The easiest
  2210.         way to do this is:
  2211.  
  2212.         ;(4.1A)
  2213.         adding1:
  2214.                clr.l  D0               ;Erase D0 (=0)
  2215.                move   table,d0         ;First entry in D0
  2216.                add    table+2,d0       ;Add second entry
  2217.                add    table+4,d0       ;Add third entry
  2218.                add    table+6,d0       ;Add fourth entry
  2219.                add    table+8,d0       ;add fifth entry
  2220.                rts                     ;Return to main program
  2221.         table: dc.w 2,4,6,8,10,
  2222.                end
  2223.  
  2224.         Try out the program using the debugger by single stepping thru the
  2225.         program until you get to the RTS instruction(left Amiga T).(SEKA
  2226.         owners use "j adding1").You see that data register D0 really
  2227.         contains the sum of the values.
  2228.         The method above only reads and adds numbers from a particular set
  2229.         of addresses.The Amigas processor has lots of different sorts of 
  2230.         addressing modes that give us a shorter and more elegant solution.
  2231.         Lets add a variable to the address of the table,so that the
  2232.         program can add different tables.
  2233.         Lets put the addresses of the table in an address register(for
  2234.         example, A0)instead.This register can be used as a pointer to the
  2235.         table.You must use move.l since only long words are relocatable.By
  2236.         using a pointer to a table you can use indirect addressing.You can
  2237.         change the expression "table+x" to"x(A0)".
  2238.  
  2239.         ;(4.1B)
  2240.         adding1:
  2241.                clr.l   D0            ;Erase D0 (=0)
  2242.                move.l  #table,a0     ;Put table addresses in A0
  2243.                move    0(a0),d0      ;Put first entry in d0
  2244.                add     2(a0),d0      ;Add second entry
  2245.                add     4(a0),d0      ;Add third entry
  2246.                add     6(a0),d0      ;Add forth entry
  2247.                add     8(a0),d0      ;Add fifth entry
  2248.                rts                   ;Return to main program]
  2249.         table: dc.w 2,4,6,8,10
  2250.                end
  2251.  
  2252.         Assemble this program,load it into the debugger.Then single step
  2253.         (left Amiga T)thru this program and you'll see that this program
  2254.         adds five numbers in order just like the last one.The reason you
  2255.         used a step size of two for the ofset is that words are two bytes
  2256.         long.AssemPro also defaults to relocate code so that you must move
  2257.         #table as a long word.
  2258.         Lets improve the program more by using "(a0)+"instead of "x(a)".
  2259.         This way,every time you access elements of the table,the address
  2260.         register A0 is automatically incremented by the number of bytes
  2261.         that are read(in this case two).The difference between this and
  2262.         the last example is that here the registers contents are modified.
  2263.         The pointer is to the next unused byte or word in memory.
  2264.         Lets make it even better.Lets make the number of words to be added
  2265.         to a variable.You'll pass the number in register D1.Now you need
  2266.         to do a different sort of programming,since you can't do it with
  2267.         the old methods.
  2268.         Lets use a loop.You need to add D1 words.You can use(a0)+ as the
  2269.         addressing method(address register indirect with post increment),
  2270.         since this automatically gets you to the next word.
  2271.         Now for the loop.You'll have D1 decremented by one every time the
  2272.         contents of the pointer are added.If D1 is zero,then you're done.
  2273.         Otherwise,you need another addition.The program looks like this:
  2274.  
  2275.         ;(4.1C)
  2276.         adding2:
  2277.                clr.l    d0            ;Erase D0
  2278.                move.l   #table,a0     ;Put table addresses in A0
  2279.                move     #$5,d1        ;Put number of entries in D1
  2280.                
  2281.         loop:                         ;Label for loop beginning
  2282.                add      (a0)+,d0      ;Add a word
  2283.                subq     #1,d1         ;Decrement counter
  2284.                bne      loop          ;Continue if non-zero
  2285.                rts                    ;Else done
  2286.  
  2287.         table: dc.w 2,4,6,8,10
  2288.                end
  2289.  
  2290.         Lets take a close look at this program.Load the pointer A0 with
  2291.         the addresses of the data and the counter D1 with the number of
  2292.         elements.Then you can single step thru the program and watch the
  2293.         results.Make sure not to run the final command,the RTS command,
  2294.         because otherwise a return address is popped from the stack,and
  2295.         the results of this are unpredictable.(SEKA owners can use"x pc"
  2296.         to point the program counter to "adding2".You can then step thru
  2297.         the program by using the "s"command and watch the results).
  2298.         To finish up this example,your assigning a little homework.Write
  2299.         the program so that it adds single bytes or long words.Try to
  2300.         write a program that takes the first value in a table and
  2301.         subtracts the following values.For example,the table
  2302.  
  2303.              table: dc.w 50,8,4,6
  2304.  
  2305.         should return the value 50-8-4-6, ie 32 ($20).
  2306.  
  2307.       4.2.Sorting a Table.
  2308.       -------------------
  2309.         Lets keep working with tables.You don't want to just read data
  2310.         from one this time.You want to change it.You'll assort the table
  2311.         in assending order.
  2312.         You need to decide how to do the sorting.The simplest method is to
  2313.         do the following.
  2314.         Compare the first and the second value.If the second value is
  2315.         larger than the first one,things are OK so far.Do the next step,
  2316.         compare the second and third values,and so on.If you come to the 
  2317.         final pair and in each case the preceding value was smaller than
  2318.         the following value,then the sorting is done(it was unnescessary).
  2319.         If you find a pair where the second value is smaller than the
  2320.         first,the two values are exchanged.You then get a flag(here let's
  2321.         use a register)that is checked once you're done going thru the
  2322.         table.If it is set,the table probably isn't completely sorted.You
  2323.         then erase the flag and start again from the beginning.If the flag
  2324.         is still zero at the end,the sorting is complete.
  2325.         Now let's write a program to do this.First let's figure out the
  2326.         variables you need.You'll use registers for the variables.You need
  2327.         a pointer to the table you're sorting(A0),a counter(D0)and a flag
  2328.         (D1).While the program is running,change these values,so you'll 
  2329.         need two more registers to store the starting values(address and
  2330.         the number of the table entries).You'll use A1 and D2.
  2331.         Let's start writing the program,each section will be written and
  2332.         then explained.Then the complete program will be given.You put the
  2333.         tables address in A1 and the number of entries in D2.
  2334.  
  2335.         ;(4.2A) part of sort routine
  2336.         sort:                       ;Start address of the program
  2337.                 move.l  #table,a1   ;Load pointer with address
  2338.                 move.l  a1,a0       ;Copy pointer to working register
  2339.                 move.l  #5,d2       ;Number in the counter
  2340.                 move.l  d2,d0       ;Copy number of elements
  2341.                 subq    #2,d0       ;Correct conter value
  2342.                 clr     d1          ;Erase flag
  2343.  
  2344.         table: dc.w 3,6,9,5
  2345.  
  2346.                end
  2347.  
  2348.         Now the preparations are complete.The pointer and the counter are
  2349.         ready and the flag is cleared.The counter is decremented by two
  2350.         because you want to use the DBRA command(take one off)and only X-1
  2351.         comparrisons are needed for X numbers(take off one more).
  2352.         Next let's write the loop that compares the values.You compare one
  2353.         word with another.It looks like this:
  2354.  
  2355.         loop:
  2356.               move   2(a0),d3   ;Next value in register D3
  2357.               cmp    (a0),d3    ;Compare values
  2358.  
  2359.         You need to use register D3 because CMP (A0),2(A0) isn't a legal
  2360.         choice.If the second value is greater than or equal to the first
  2361.         value,you can skip an exchange.
  2362.  
  2363.               bcc    noswap      ;Branch if greater than or equal
  2364.                                  ;to
  2365.  
  2366.         Now you need to do the exchanging(unfortunatly you can't use EXC
  2367.         2(a0),(a0) since this form of addressing does not exist).
  2368.  
  2369.         doswap:
  2370.                move   (a0),d1      ;Save first valus
  2371.                move   2(a0),(a0)   ;Copy second into first word
  2372.                move   d1,2(a0)     ;Move first into second
  2373.                moveq  #1,d1        ;Set flag
  2374.         noswap:
  2375.  
  2376.         Now increment the counter and continue with the next pair.You do
  2377.         this until the counter is negative.
  2378.  
  2379.                addq.l  #2,a0      ;Pointer+2
  2380.                dbra    d0,loop    ;Continuing looping until the end
  2381.  
  2382.         Now you'll see if the flag is set.You start again at the beginning
  2383.         if it is.
  2384.  
  2385.                tst     d1         ;Test flag
  2386.                bne     sort       ;Not finished sorting yet!
  2387.                rts                ;Otherwise done.Return
  2388.  
  2389.         If the flag is zero,you're done and the subroutine ends.You jump
  2390.         back to the main program using the RTS command.
  2391.         Now a quick overview of the complete program.
  2392.  
  2393.         ;(4.2B)
  2394.         sort:                      ;Start address of the program
  2395.               move.l  #table,a1    ;Load pointer with address
  2396.               move.l  a1,a0        ;Copy pointer to working register
  2397.               move.l  #5,d2        ;Number in the counter
  2398.               move.l  d2,d0        ;Copy number of elements
  2399.               subq    #2,d0        ;Correct counter value
  2400.               clr     d1           ;Erase flag
  2401.  
  2402.         loop:
  2403.               move    2(a0),d3     ;Next value in register D3
  2404.               cmp     (a0),d3      ;Compare values
  2405.               bcc     noswap       ;Branch if greater than or equal to
  2406.  
  2407.         doswap:
  2408.               move    (a0),d1      ;Save first value
  2409.               move    2(a0),(a0)   ;Copy second into first word
  2410.               move    d1,2(a0)     ;Move first into second
  2411.               moveq   #1,d1        ;Set flag
  2412.  
  2413.         noswap: 
  2414.               addq.l  #2,a0        ;Pointer+2
  2415.               dbra    do,loop      ;Continue looping until the end
  2416.               tst     d1           ;Test flag
  2417.               bne     sort         ;Not finished sorting yet!
  2418.               rts                  ;Otherwise done.Return
  2419.  
  2420.         table: 
  2421.               dc.w    10,8,6,4,2   ;When finished acceding
  2422.  
  2423.               end
  2424.  
  2425.         To test this subroutine,assemble the routine with AssemPro,save it
  2426.         and then load it into the debugger.The table is directly after the
  2427.         RTS,notice its order.Set a breakpoint at the RTS,select the
  2428.         address with the mouse and press left-Amiga-B sets a breakpoint in
  2429.         AssemPro.Start the program the redisplay the screen by selecting
  2430.         "Parameter-Display-Dissassem-bled"and examine the order of the
  2431.         numbers in the table,they should now be ascending order.
  2432.         You use several registers in this example for storing values.
  2433.         Usually in machine language programming your subroutines cannot
  2434.         change any register or can only change certain registers.For this
  2435.         reason,there is a machine language command to push several
  2436.         registers onto the stack at the same time.This is the MOVEM ("MOVE
  2437.         multiple")command.If you insert this command twice in your program
  2438.         then you can have the registers return to the main program with
  2439.         the values they had when the subroutine was called.To do this,you
  2440.         need one more label.Let's call it"start";the subroutine is started
  2441.         from here.
  2442.  
  2443.         start:
  2444.               movem.l  d0-d7/a0-a6,-(sp)    ;save registers
  2445.               
  2446.         sort:
  2447.               etc...
  2448.               ...
  2449.               ...
  2450.               bne      sort                 ;not finished sorting yet!
  2451.  
  2452.               movem.l  (sp)+,d0-d7/a0-a6    ;retrieve registers
  2453.               rts                           ;finished
  2454.  
  2455.         The powerful command moves several registers at the same time.You
  2456.         can specify which registers should be moved.If you want to move
  2457.         the D1,D2,D3,D7,A2 and A3 registers,just write 
  2458.  
  2459.               movem.l  d1-d3/d7/a2-a3,-(sp)
  2460.  
  2461.         Before you quit sorting,do one little homework assignment.Modify
  2462.         the program,so that it sorts the elements in descending order.
  2463.  
  2464.       4.3.Converting Number Systems.
  2465.       -----------------------------
  2466.         As we mentioned in the chapter on number systems,converting
  2467.         numbers from one base to another can be rather difficult.There is
  2468.         another form of numeric representation-as a string that can be
  2469.         entered via the keyboard or output on the screen.
  2470.         You want to look at some of the many conversions possible and
  2471.         write programs to handle the task.You'll start by converting a hex
  2472.         number into a string using binary numbers and then print the value
  2473.         in hex.
  2474.  
  2475.       4.3.1.Converting Hex To ASCII.
  2476.       -----------------------------
  2477.         First you need to set the start and finish conditions.In this
  2478.         example,let's assume that data register D1 contains a long word
  2479.         that should be converted into a 8-digit long string of ASCII
  2480.         characters.You'll write it to a particular memory location so that
  2481.         you can output it later.
  2482.         The advantage of using hex instead of decimal is pretty clear in
  2483.         this example.To find out the hexadecimal digit for a particular
  2484.         spot in the number,you just need to take the corresponting 4 bits
  2485.         (half byte)and do some work on it.A half byte(also called a
  2486.         nibble)contains one hex digit.
  2487.         You'll work on a half byte in D2.To convert this to a printable
  2488.         character,you need to use the correct ASCII code.The codes of the
  2489.         16 characters that are used as hex digits are the following:
  2490.  
  2491.           0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
  2492.  
  2493.         $30 $31 $32 $33 $34 $35 $36 $37 $38 $39 $41 $42 $43 $44 $45 $46
  2494.  
  2495.         To convert the digits 0-9,you just need to add $30.For the letters
  2496.         A-F that correspond to the values 10-15,you need to add $37.The
  2497.         program to evaluate a half byte must make a destinction between
  2498.         values between 0 and 9 and those between A and F and add either
  2499.         $30 or $37.
  2500.         Now let's write a machine language subroutine that you'll call for
  2501.         each digit in the long words hex representation.
  2502.  
  2503.         nibble:
  2504.                and    #$0f,d2     ;just keep low byte
  2505.                add    #$30,d2     ;add $30
  2506.                cmp    #$3a,d2     ;was it a digit?
  2507.                bcs    ok          ;yes:done
  2508.                add    #7,d2       ;else add 7
  2509.  
  2510.         ok:
  2511.                rts                ;done
  2512.  
  2513.         This routine converts the nibble in D2 to an ASCII character that
  2514.         corresponds to the hex value of the nibble.To convert an entire
  2515.         byte,you need to call the routine twice.Here is a program to do
  2516.         this.The program assumes that A0 contains the address of the
  2517.         buffer that the characters are to be put in and that D1 contains
  2518.         the byte that is converted.
  2519.  
  2520.         ;(4.3.1a) bin-hex
  2521.         ;                            ;your program
  2522.                  lea     buffer,a0   ;pointer to buffer
  2523.                  move    #$4a,d1     ;byte to be converted(example)
  2524.                  bsr     byte        ;and convert
  2525.                  rts
  2526.         ;        ...                 ;more of your program
  2527.         byte:
  2528.                  move    d1,d2       ;move value into d2
  2529.                  lsr     #4,d2       ;move upper nibble into lower nibble
  2530.                  bsr     nibble      ;convert d2
  2531.                  move.b  d2,(a0)+    ;put character into buffer
  2532.                  move    d1,d2       ;value in d2
  2533.                  bsr     nibble      ;convert lower nibble
  2534.                  move.b  d2,(a0)+    ;and put it in buffer
  2535.                  rts                 ;done
  2536.         nibble:
  2537.                  and     #$0f,d2     ;just keep low byte
  2538.                  add     #$30,d2     ;add $30
  2539.                  cmp     #$3a,d2     ;was it a digit?
  2540.                  bcs     ok          ;yes:done
  2541.                  add     #7,d2       ;else add 7
  2542.         ok:
  2543.                  rts                 ;done
  2544.         buffer:
  2545.                  blk.b   9,0         ;space for long word data
  2546.  
  2547.                  end
  2548.  
  2549.         To test this subroutine,use AssemPro to assemble the routine,save
  2550.         the program and load it intoi the debugger.Next set a breakpoint
  2551.         at the first RTS,to set the breakpoint in AssemPro select the
  2552.         correct address with the mouse and press the right-Amiga-B keys.
  2553.         Start the program and watch the contents of D2,it is first $34
  2554.         (ASCII 4)and finally $41(ASCII A).Select "Parameter-Display-HEX-
  2555.         Dump"and you'll see that 4A has been moved into the buffer.
  2556.         This is how the routine operates.First,you move the value that you
  2557.         wish to convert into D2.Then you shift the register four times to
  2558.         the right to move the upper nibble into the lower four bits.After
  2559.         the subroutine call,you use "move.b d2,(a0)+"to put the HI nibble
  2560.         in the buffer.Then the original byte is put in D2 again.It is 
  2561.         converted.This gives us the LO nibble as an ASCII character in D2.
  2562.         You put this in the next byte of the buffer.
  2563.         The buffer is long enough to hold a long word in characters and
  2564.         closing the null byte.The null byte is usually required by screen
  2565.         output routines.Screen output will be discussed in a later
  2566.         chapter.Now let's worry about converting a long word.
  2567.         When converting a long word,you need to be sure to deal with the
  2568.         nibbles in the right order.Before calling the "nibble"routine for
  2569.         the first time,you need to move the upper nibble into the lower 4
  2570.         bits of the long word.You need to do this without losing anything.
  2571.         The LSR command isn't very good for this application.If you use it
  2572.         you'll lose bits.Its better to use the rotation commands like ROR
  2573.         or ROL,since they move the bits that are shifted out,back in on
  2574.         the other side.
  2575.         If you shift the original long word in D1 four times to the left,
  2576.         the upper four bits are shifted into the lower four bits.Now you
  2577.         can use our "nibble"routine to evaluate it and then put the
  2578.         resulting ASCII character in the buffer.You repeat this eight
  2579.         times and the whole long word has been converted.You even have D1
  2580.         looking exactly the way it did before the conversion process began
  2581.  
  2582.         ;(4.3.1B) bin-hex-2
  2583.         hexlong:
  2584.                lea     buffer,a0      ;pointer to the buffer
  2585.                move.l  #$12345678,d1  ;data to convert
  2586.                move    #7,d3          ;counter for the nibbles:8-1
  2587.  
  2588.         loop:
  2589.                rol     #4,d1          ;move upper nibble into lower
  2590.                move    d1,d2          ;write in d2
  2591.                bsr     nibble         ;and convert it
  2592.                move.b  d2,(a0)+       ;character in buffer
  2593.                dbra    d3,loop        ;repeat 8 times
  2594.                rts                    ;finished!
  2595.  
  2596.         nibble:
  2597.                and     #$0f,d2        ;just keep low byte
  2598.                add     #$30,d2        ;add $30
  2599.                cmp     #$3a,d2        ;was it a digit?
  2600.                bcs     ok             ;yes:done
  2601.                add     #7,d2          ;else add 7
  2602.  
  2603.         ok:
  2604.                rts                    ;done
  2605.  
  2606.         buffer:
  2607.                blk.b   9,0            ;space for long word,null byte
  2608.  
  2609.                end
  2610.  
  2611.         To test this subroutine,use AssemPro to assemble the routine,save
  2612.         the program and load it into the debugger.Next set a breakpoint at
  2613.         the first RTS,to set the breakpoint in AssemPro select the correct
  2614.         address with the mouse and press the right-Amiga-B keys.Start the
  2615.         program and when it is finished redisplay the output by selecting
  2616.         "Parameter-Display-HEX-dump"so you can examine the new buffer
  2617.         contents.
  2618.         You'll find that there's an error in the program-the buffer
  2619.         contains the digits "56785678"instead of "12345678".Try to find
  2620.         the error.
  2621.         Have you found it?This is the sort of error that causes you to
  2622.         start pulling your hair out.This sort is hard to find.The
  2623.         assembler assumes that the rotation operation should be done on a
  2624.         word,because the ".l"was left off.As a result,only the lower word
  2625.         of D1 was rotated-so you get the same value twice.If you change
  2626.         the "rol.l",things work just right.
  2627.         The error shows how easy it is to convert the program above into
  2628.         one that converts four digit hex numbers into ASCII characters.
  2629.         Just leave off the ".l"on the "rol"command and change the counter
  2630.         from seven to three.The program is done.
  2631.         Now for a little homework:change the program so that it can handle
  2632.         six digit hex numbers(D1 doesn't nescessarily have to stay the
  2633.         same...)!
  2634.         Now lets look at a different conversion problem:converting a four
  2635.         digit decimal number.
  2636.  
  2637.       4.3.2.Converting Decimal To ASCII.
  2638.       ---------------------------------
  2639.         It's not quite as easy to convert decimal as hex.You can't group
  2640.         the bits to form individual digits.You need to use another method.
  2641.         Lets look at how a decimal number is constructed.In a four digit
  2642.         number,the highest place is in the thousands place,the next is the
  2643.         hundreds place,etc...
  2644.         If you have the value in a register and divide by 1000,you'll get
  2645.         the value that goes in the highest place in the decimal number.
  2646.         Since the machine language command DIV not only gives us the
  2647.         result of the division but also gives us the remainder,you can
  2648.         work out the remainder quite easily.You divide the remainder by
  2649.         100 to find the hundreds place,divide the remainder by 10 and get
  2650.         the tens place,and the final remainder is the ones place.
  2651.         This isn't so hard after all!Heres the program that follows the
  2652.         steps above to fill the buffer with D1's ASCII value.
  2653.  
  2654.         main:
  2655.                lea     buffer,a0    ;pointer to the buffer
  2656.                move    #1234,d1     ;number to convert
  2657.                jsr     deci_4       ;test subroutine
  2658.                illegal              ;room for breakpoint
  2659.  
  2660.         deci_4:                     ;subroutine-four digit numbers
  2661.  
  2662.                divu    #1000,d1     ;divide by 1000
  2663.                bsr     digit        ;evaluate result-move remainder
  2664.  
  2665.                divu    #100,d1      ;divide by 100
  2666.                bsr     digit        ;evaluate result and move
  2667.  
  2668.                divu    #10,d1       ;divide by 10
  2669.                bsr     digit        ;evaluate result-move remainder
  2670.  
  2671.                                     ;evaluate the remainder directly
  2672.  
  2673.         digit:
  2674.                add     #$30,d1      ;convert result into ASCII
  2675.                move.b  d1,(a0)+     ;move it into buffer
  2676.                clr     d1           ;erase lower word
  2677.                swap    d1           ;move the remainder down
  2678.                rts                  ;return
  2679.  
  2680.         buffer:blk.b 5,0            ;reserve bytes for result
  2681.  
  2682.                end
  2683.  
  2684.         To test this subroutine,use AssemPro to assemble the routine,save
  2685.         the program and load it into the debugger.Next set a breakpoint at
  2686.         the illegal instruction.To set the breakpoint in AssemPro select
  2687.         the correct address with the mouse and press the right-Amiga-B
  2688.         keys.This breakpoint stops the program.Start the program and when
  2689.         it is finished redisplay the output by selecting"Parameter-Display
  2690.         -HEX-dump"so you can examine the ASCII values now in the buffer.
  2691.         You use a little trick in this program that is typical for machine
  2692.         language programming.After calling"digit"three times from the sub-
  2693.         routine"deci_4",you go right into the"digit"subroutine.You don't 
  2694.         use a BSR or JSR command.Once the processor hits the RTS command,
  2695.         it returns to the main program,not the "deci_4"subroutine.Doing
  2696.         this,you save a fourth "bsr digit"command and an "rts"command for
  2697.         the "deci_4"routine.
  2698.         Try the program out.Make sure that you use values that are smaller
  2699.         than 9999,because otherwise strange things can happen.
  2700.         Now let's reverse what you've been doing and convert strings into
  2701.         binary numbers.
  2702.  
  2703.       4.3.3.Converting ASCII To Hex.
  2704.       -----------------------------
  2705.         In a string,each hex digit represents a half byte.You just need to
  2706.         write a program that exactly reverses what the hex conversion
  2707.         program did.
  2708.         You have two choices
  2709.  
  2710.           1.  The number of hex digits is known in advance
  2711.           2.  The number is unknown
  2712.  
  2713.         The first is easier to program,but has the disadvantage that if,
  2714.         you assume the strings are four digits in length and want to enter
  2715.         the value 1,you must enter 0001.That is rather awkward,so you'll
  2716.         use the second method.
  2717.         Let's convert a single digit first.You'll pass a pointer to this
  2718.         digit in address register A0.You want the binary value to come
  2719.         back in data register D0.
  2720.         The program looks like this:
  2721.  
  2722.                move.l  #string,a0    ;this example
  2723.                jsr     nibblein      ;test routine
  2724.                nop                   ;set breakpoint here
  2725.  
  2726.         nibblein:                    ;*convert the nibble from (A0)
  2727.                clr.l   d0            ;erase D0
  2728.                move.b  (a0)+,d0      ;get digit,increment A0
  2729.                sub     #'A',d0       ;subtract $41
  2730.                bcc     ischar        ;no problem:in the range A-F
  2731.  
  2732.                add     #7,d0         ;else correct value
  2733.         ischar:
  2734.                add     #10,d0        ;correct value
  2735.                rts
  2736.  
  2737.         string:dc.b 'B',0            ;character to convert
  2738.  
  2739.                end
  2740.  
  2741.         To test this subroutine,use AssemPro to assemble the routine,save
  2742.         the program and load it into the debugger.Next set a breakpoint at
  2743.         the first NOP,to set the breakpoint in AssemPro select the correct
  2744.         address with the mouse and press the right-Amiga-B keys.Start the
  2745.         program and watch the contents of D0.
  2746.         Let's see how the program works.A0 points to a memory location
  2747.         that contains the character "B"that is represented by the ASCII
  2748.         value $42.This number is loaded into D0 right after this register
  2749.         is erased.
  2750.         After subtracting $41,you end up with the value $1.Now you're
  2751.         almost done.Before returning to the main program,you add 10 to get
  2752.         the correct value 11,$B.
  2753.         If the buffer has a digit in it,the subtraction causes the
  2754.         register to become negative.The C flag is set.Let's take the digit
  2755.         5 as an example.
  2756.         The ASCII value of 5 is $35.After subtracting $41,you end up with
  2757.         -12 and the C flag is set.In this case,you won't branch with the
  2758.         BCC command.Instead you'll add 7 to get -5.Then 10 is added,and
  2759.         you end up with 5.Done!
  2760.         The routine as a disadvantage.If an illegal character is given,one
  2761.         that doesn't represent a hex digit,you'll get some nonsense result
  2762.         Let's ignore error checking for the moment though.
  2763.         Let's go on to multi-digit hex numbers.The first digit that you
  2764.         convert has the highest value and thus represents the highest
  2765.         nibble.To allow for this and to allow for an arbitrarily long
  2766.         number(actually not arbitrarily long,the number should fit in a
  2767.         long word-so it can only be eight digits long),you'll use a trick.
  2768.         Take a look at the whole program.It handles the calculations and
  2769.         puts the result in D1.It assumes that A0 is a pointer to a string
  2770.         and that this string is ended by null byte.
  2771.  
  2772.         hexin:                       ;converting a hex number
  2773.                clr.l   d1            ;first erase D1
  2774.                move.l  #string,a0    ;address of the string in A0
  2775.                jsr     hexinloop     ;test subroutine
  2776.                nop                   ;set breakpoint here
  2777.  
  2778.         hexinloop:
  2779.                tst.b   (a0)          ;test digit
  2780.                beq     hexinok       ;zero,then done
  2781.                bsr     nibblein      ;convert digit
  2782.                lsl.l   #4,d1         ;shift result
  2783.                or.b    d0,d1         ;insert nibble
  2784.                bra     hexinloop     ;and continue
  2785.  
  2786.         hexinok:
  2787.                rts
  2788.  
  2789.         nibblein:
  2790.                clr.l   d0            ;convert the nibble from (A0)
  2791.                move.b  (a0)+,d0      ;get digit,increment A0
  2792.                sub     #'A',d0       ;subtract $41
  2793.                bcc     ischar        ;no problem:in range A-F
  2794.                add     #7,d0         ;else correct value
  2795.           
  2796.         ischar:
  2797.                add     #10,d0        ;correct value
  2798.                rts
  2799.  
  2800.         string:DC.B "56789ABC',00    ;eight digit string,null byte
  2801.                                      ;to be converted
  2802.                end
  2803.  
  2804.         To test this subroutine,use AssemPro to assemble the routine,save
  2805.         the program and load it into the debugger.Next set a breakpoint at
  2806.         the NOP,to set the breakpoint in AssemPro select the correct
  2807.         address with the mouse and press the right-Amiga-B keys.Start the
  2808.         program and watch the contents of D1,the hex value is placed in
  2809.         this register.
  2810.         The trick is to shift left four times,to shift one nibble.In this
  2811.         way,the place of the last digit is incremented by one and there is
  2812.         room for the nibble that comes back from the "nibblein"routine.The
  2813.         program uses the TST.B instruction to check for the null byte at
  2814.         the end of the string,when it encounters the null byte the program
  2815.         ends.The result is in the D1 long word already!
  2816.         To do some error checking,you need to make some changes in the
  2817.         program.You'll do this right after you come back from the"nibblin"
  2818.         routine with the value of the current character.
  2819.         If the value in D0 is bigger than $F,there is an error.You can
  2820.         detect this in several ways.You chose the simplest one-you'll use
  2821.         CMP #$10,D0 to compare D0 with $10.If it smaller,then the C flag
  2822.         is set(since CMP uses subtraction)and everything is fine.If C is
  2823.         zero,there is an error.
  2824.         You can use this trick to skip the test for a null byte,since its
  2825.         an invalid character as well.The program looks like this:
  2826.  
  2827.         ;(4_3_3C) hex-conv2         optional disk name
  2828.         hexin:                      ;converting a hex number
  2829.                clr.l   d1           ;first erase D1
  2830.                move.l  #string,a0   ;address of string in A0
  2831.                jsr     hexinloop    ;test subroutine
  2832.                nop                  ;set breakpoint here
  2833.  
  2834.  
  2835.         hexinloop:
  2836.                bsr     nibblein     ;convert digit
  2837.                cmp     $10,d0       ;test if good
  2838.                bcc     hexinok      ;no,then done
  2839.                lsl.l   #4,d1        ;shift result
  2840.                or.b    d0,d1        ;insert nibble
  2841.                bra     hexinloop    ;and continue
  2842.  
  2843.         hexinok:
  2844.                rts
  2845.  
  2846.         nibblein:                   ;convert the nibble from (A0)
  2847.                clr.l   d0           ;erase D0
  2848.                move.b  (a0)+,d0     ;get digit,increment A0
  2849.                sub     #'A',d0      ;subtract $41
  2850.                bcc     ischar       ;no problem:in the range A-F
  2851.                add     #7,d0        ;else correct value
  2852.  
  2853.         ischar:
  2854.                add     #10,d0       ;correct value
  2855.                rts
  2856.  
  2857.         string:DC.B "56789ABC',00   ;8 digit string ending with a
  2858.                                     ;null byte to be converted
  2859.                end
  2860.  
  2861.         To test this subroutine,use AssemPro to assemble the routine,save
  2862.         the program and load it into the debugger.Next set a breakpoint at
  2863.         the NOP,to set the breakpoint in AssemPro select the correct
  2864.         address with the mouse and press the right-Amiga-B keys.Start the
  2865.         program and watch the contents of D1,the hex value is placed in
  2866.         this register.
  2867.         This is the method for converting hex to binary.If you convert
  2868.         decimal to binary,the conversion is not much harder.
  2869.  
  2870.       4.3.4.Converting ASCII To Decimal.
  2871.       ---------------------------------
  2872.         You can use a very similar method to the one used above.Since you
  2873.         are not sure how many digits there are,you'll use a similar method
  2874.         for putting digits of a number in the next place up.You can't do
  2875.         this with shifting,but you can multiply by 10 and add the value of
  2876.         the digit.
  2877.         Heres the program for converting decimal numbers.
  2878.  
  2879.         decin:                      ;converting a decimal number
  2880.                clr.l   d1           ;first erase D1
  2881.                move.l  #string,a0   ;the string to convert
  2882.                jsr     decinloop    ;test subroutine
  2883.                nop                  ;breakpoint here
  2884.  
  2885.         decinloop:
  2886.                bsr     digitin      ;convert digit
  2887.                cmp     #10,d0       ;test,if valid
  2888.                bcc     decinok      ;no,then done
  2889.                mulu    #10,d1       ;shift result
  2890.                add     d0,d1        ;insert nibble
  2891.                bra     decinloop    ;and continue
  2892.  
  2893.         decinok:
  2894.                rts                  ;end of conversion
  2895.  
  2896.         digitin:                    ;converting the nibble from (A0)
  2897.  
  2898.                clr.l   d0           ;erase D0
  2899.                move.b  (a0)+,d0     ;get digit,increment A0
  2900.                sub     #'0',d0      ;subtract $30
  2901.                rts
  2902.  
  2903.         string:dc.b '123456'        ;ASCII decimal string to convert
  2904.  
  2905.  
  2906.                end
  2907.  
  2908.         To test this subroutine,use AssemPro to assemble the routine,save
  2909.         the program and load it into the debugger.Next set a breakpoint at
  2910.         the NOP,to set the breakpoint in AssemPro select the correct
  2911.         address with the mouse and press thr right-Amiga-B keys.Select
  2912.         "Parameter-Output-numbers-Decimal"so the registers are displayed
  2913.         as decimal numbers.Then start the program and watch the contents
  2914.         of D1,the decimal value is placed in this register.
  2915.         This program can ONLY convert numbers upto 655350,although the hex
  2916.         conversion routine can go higher.Thats because the MULU command
  2917.         can only multiply 16-bit words.The last multiplication that can be
  2918.         done correctly is $FFFF*10--65535*10,which gives us the value
  2919.         655350.Normally this is a large enough range,so you won't
  2920.         complicate the program further. 
  2921.  
  2922.         
  2923.  
  2924.                             NOW LOAD PART 2
  2925. end.
  2926.